Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Model {
- prop1: number;
- }
- class A<TModel> {
- constructor(p: (model: TModel) => any) {}
- bar = (): A<TModel> => {
- return this;
- }
- }
- function foo<T>(p: A<Model>) { }
- foo(new A(x => x.prop1)) // Works
- foo(new A<Model>(x => x.prop1).bar()) // Works
- foo(new A(x => x.prop1).bar()) // Doesn't work. (Property 'prop1' does not exist on type '{}'.)
- declare const x: string;
- declare function f<T>(x: T): Array<T>;
- const y = f(x); // y inferred as Array<string>;
- declare function anyX<T>(): T;
- const someX = anyX(); // inferred as {}
- declare function f<T>(x: T): Array<T>;
- const y: Array<string> = f(anyX()); // anyX() inferred as string
- const z: Array<Array<Array<string>>> = f(f(f(anyX())));
- type G<T> = { g: T; }
- declare function anyG<T>(): G<T>;
- const h: string = anyG().g; // error! no contextual typing here
- const h: string = anyG<string>().g; // okay
- class Model {
- prop1!: number;
- }
- class A<TModel> {
- constructor(p: (model: TModel) => any) {}
- bar = (): A<TModel> => {
- return this;
- };
- }
- function foo(p: A<Model>) {}
- // helper function to get some contextual typing
- function fixIt<TModel>(p: (model: TModel) => any): A<TModel> {
- return new A(p).bar();
- }
- foo(fixIt(x => x.prop1)); // okay now
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement