typescript-book icon indicating copy to clipboard operation
typescript-book copied to clipboard

Generic instantiation

Open basarat opened this issue 7 years ago • 0 comments

In type space using type e.g.

type Foo<T> = {foo: T}
type Bar = Foo<number>;

In variables space (investigating):

// A function that takes argument of type `T`
// And returns an object of type `{foo:T}`
const foo = <T>(x: T) => ({ foo: x });

// Instantiate `T` as number in a variable bar
const bar = foo<number>; // Error: Invalid syntax 

basarat avatar Oct 08 '18 22:10 basarat