Chapter 1, Becoming Functional – Several Questions
1.1 TypeScript, please! The following are the fully annotated versions of the code in the chapter. This is the code for the factorial functions:
// question_01_typescript_please.ts function fact(n: number): number { if (n === 0) { return 1; } else { return n * fact(n - 1); } } const fact2 = (n: number): number => { if (n === 0) { return 1; } else { return n * fact2(n - 1); } }; const fact3 = (n: number): number => n === 0 ? 1 : n * fact3(n - 1);
This is the code for the spreading examples:
// continued... function sum3(a: number, b: number, c: number): number { return a + b + c; } const x: [number, number, number] = [1, 2, 3]; const y = sum3(...x); // equivalent to sum3(1,2,3) const f = [1, 2, 3]; const g = [4, ...f, 5];...