206 lines
6.2 KiB
Text
206 lines
6.2 KiB
Text
=== tests/cases/conformance/expressions/contextualTyping/functionExpressionContextualTyping1.ts ===
|
|
// When a function expression with no type parameters and no parameter type annotations
|
|
// is contextually typed (section 4.19) by a type T and a contextual signature S can be extracted from T
|
|
|
|
enum E { red, blue }
|
|
>E : E
|
|
>red : E
|
|
>blue : E
|
|
|
|
// A contextual signature S is extracted from a function type T as follows:
|
|
// If T is a function type with exactly one call signature, and if that call signature is non- generic, S is that signature.
|
|
|
|
var a0: (n: number, s: string) => number = (num, str) => {
|
|
>a0 : (n: number, s: string) => number
|
|
>n : number
|
|
>s : string
|
|
>(num, str) => { num.toExponential(); return 0;} : (num: number, str: string) => number
|
|
>num : number
|
|
>str : string
|
|
|
|
num.toExponential();
|
|
>num.toExponential() : string
|
|
>num.toExponential : (fractionDigits?: number) => string
|
|
>num : number
|
|
>toExponential : (fractionDigits?: number) => string
|
|
|
|
return 0;
|
|
>0 : number
|
|
}
|
|
|
|
class Class<T> {
|
|
>Class : Class<T>
|
|
>T : T
|
|
|
|
foo() { }
|
|
>foo : () => void
|
|
}
|
|
|
|
var a1: (c: Class<Number>) => number = (a1) => {
|
|
>a1 : (c: Class<Number>) => number
|
|
>c : Class<Number>
|
|
>Class : Class<T>
|
|
>Number : Number
|
|
>(a1) => { a1.foo(); return 1;} : (a1: Class<Number>) => number
|
|
>a1 : Class<Number>
|
|
|
|
a1.foo();
|
|
>a1.foo() : void
|
|
>a1.foo : () => void
|
|
>a1 : Class<Number>
|
|
>foo : () => void
|
|
|
|
return 1;
|
|
>1 : number
|
|
}
|
|
|
|
// A contextual signature S is extracted from a function type T as follows:
|
|
// If T is a union type, let U be the set of element types in T that have call signatures.
|
|
// If each type in U has exactly one call signature and that call signature is non- generic,
|
|
// and if all of the signatures are identical ignoring return types,
|
|
// then S is a signature with the same parameters and a union of the return types.
|
|
var b1: ((s: string, w: boolean) => void) | ((s: string, w: boolean) => string);
|
|
>b1 : ((s: string, w: boolean) => void) | ((s: string, w: boolean) => string)
|
|
>s : string
|
|
>w : boolean
|
|
>s : string
|
|
>w : boolean
|
|
|
|
b1 = (k, h) => { };
|
|
>b1 = (k, h) => { } : (k: string, h: boolean) => void
|
|
>b1 : ((s: string, w: boolean) => void) | ((s: string, w: boolean) => string)
|
|
>(k, h) => { } : (k: string, h: boolean) => void
|
|
>k : string
|
|
>h : boolean
|
|
|
|
var b2: typeof a0 | ((n: number, s: string) => string);
|
|
>b2 : ((n: number, s: string) => number) | ((n: number, s: string) => string)
|
|
>a0 : (n: number, s: string) => number
|
|
>n : number
|
|
>s : string
|
|
|
|
b2 = (foo, bar) => { return foo + 1; }
|
|
>b2 = (foo, bar) => { return foo + 1; } : (foo: number, bar: string) => number
|
|
>b2 : ((n: number, s: string) => number) | ((n: number, s: string) => string)
|
|
>(foo, bar) => { return foo + 1; } : (foo: number, bar: string) => number
|
|
>foo : number
|
|
>bar : string
|
|
>foo + 1 : number
|
|
>foo : number
|
|
>1 : number
|
|
|
|
b2 = (foo, bar) => { return "hello"; }
|
|
>b2 = (foo, bar) => { return "hello"; } : (foo: number, bar: string) => string
|
|
>b2 : ((n: number, s: string) => number) | ((n: number, s: string) => string)
|
|
>(foo, bar) => { return "hello"; } : (foo: number, bar: string) => string
|
|
>foo : number
|
|
>bar : string
|
|
>"hello" : string
|
|
|
|
var b3: (name: string, num: number, boo: boolean) => void;
|
|
>b3 : (name: string, num: number, boo: boolean) => void
|
|
>name : string
|
|
>num : number
|
|
>boo : boolean
|
|
|
|
b3 = (name, number) => { };
|
|
>b3 = (name, number) => { } : (name: string, number: number) => void
|
|
>b3 : (name: string, num: number, boo: boolean) => void
|
|
>(name, number) => { } : (name: string, number: number) => void
|
|
>name : string
|
|
>number : number
|
|
|
|
var b4: (n: E) => string = (number = 1) => { return "hello"; };
|
|
>b4 : (n: E) => string
|
|
>n : E
|
|
>E : E
|
|
>(number = 1) => { return "hello"; } : (number?: E) => string
|
|
>number : E
|
|
>1 : number
|
|
>"hello" : string
|
|
|
|
var b5: (n: {}) => string = (number = "string") => { return "hello"; };
|
|
>b5 : (n: {}) => string
|
|
>n : {}
|
|
>(number = "string") => { return "hello"; } : (number?: {}) => string
|
|
>number : {}
|
|
>"string" : string
|
|
>"hello" : string
|
|
|
|
// A contextual signature S is extracted from a function type T as follows:
|
|
// Otherwise, no contextual signature can be extracted from T and S is undefined.
|
|
var b6: ((s: string, w: boolean) => void) | ((n: number) => number);
|
|
>b6 : ((s: string, w: boolean) => void) | ((n: number) => number)
|
|
>s : string
|
|
>w : boolean
|
|
>n : number
|
|
|
|
var b7: ((s: string, w: boolean) => void) | ((s: string, w: number) => string);
|
|
>b7 : ((s: string, w: boolean) => void) | ((s: string, w: number) => string)
|
|
>s : string
|
|
>w : boolean
|
|
>s : string
|
|
>w : number
|
|
|
|
b6 = (k) => { k.toLowerCase() };
|
|
>b6 = (k) => { k.toLowerCase() } : (k: any) => void
|
|
>b6 : ((s: string, w: boolean) => void) | ((n: number) => number)
|
|
>(k) => { k.toLowerCase() } : (k: any) => void
|
|
>k : any
|
|
>k.toLowerCase() : any
|
|
>k.toLowerCase : any
|
|
>k : any
|
|
>toLowerCase : any
|
|
|
|
b6 = (i) => {
|
|
>b6 = (i) => { i.toExponential(); return i;} : (i: any) => any
|
|
>b6 : ((s: string, w: boolean) => void) | ((n: number) => number)
|
|
>(i) => { i.toExponential(); return i;} : (i: any) => any
|
|
>i : any
|
|
|
|
i.toExponential();
|
|
>i.toExponential() : any
|
|
>i.toExponential : any
|
|
>i : any
|
|
>toExponential : any
|
|
|
|
return i;
|
|
>i : any
|
|
|
|
}; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
|
|
b7 = (j, m) => { }; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
|
|
>b7 = (j, m) => { } : (j: any, m: any) => void
|
|
>b7 : ((s: string, w: boolean) => void) | ((s: string, w: number) => string)
|
|
>(j, m) => { } : (j: any, m: any) => void
|
|
>j : any
|
|
>m : any
|
|
|
|
class C<T, U> {
|
|
>C : C<T, U>
|
|
>T : T
|
|
>U : U
|
|
|
|
constructor() {
|
|
var k: ((j: T, k: U) => (T|U)[]) | ((j: number,k :U) => number[]) = (j, k) => {
|
|
>k : ((j: T, k: U) => (T | U)[]) | ((j: number, k: U) => number[])
|
|
>j : T
|
|
>T : T
|
|
>k : U
|
|
>U : U
|
|
>T : T
|
|
>U : U
|
|
>j : number
|
|
>k : U
|
|
>U : U
|
|
>(j, k) => { return [j, k]; } : (j: any, k: any) => any[]
|
|
>j : any
|
|
>k : any
|
|
|
|
return [j, k];
|
|
>[j, k] : any[]
|
|
>j : any
|
|
>k : any
|
|
|
|
} // Per spec, no contextual signature can be extracted in this case.
|
|
}
|
|
}
|