...
/Generic Return Type, Optional Parameter and Default Value
Generic Return Type, Optional Parameter and Default Value
In this lesson, you will cover the concept of the generic return type, optional parameter, and default value.
We'll cover the following...
Function’s generic return type
TypeScript, since version 2.4, can infer the type of a function’s return value. Before version 2.4, s.length
would give an error. The s
was not from a U[]
type but an empty object literal {}.
Press + to interact
function arrayMap<T, U>(f: (x: T) => U): (a: T[]) => U[] {return a => a.map(f);}const lengths: (a: string[]) => number[] = arrayMap(s => s.length);
Function’s optional parameters
TypeScript lets you have optional parameters with different syntax. The ...