TypeScript/tests/cases/conformance/expressions/contextualTyping/functionExpressionContextualTyping1.ts
2015-04-16 15:39:12 -07:00

56 lines
No EOL
2.4 KiB
TypeScript

// 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 }
// 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) => {
num.toExponential();
return 0;
}
class Class<T> {
foo() { }
}
var a1: (c: Class<Number>) => number = (a1) => {
a1.foo();
return 1;
}
// 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 = (k, h) => { };
var b2: typeof a0 | ((n: number, s: string) => string);
b2 = (foo, bar) => { return foo + 1; }
b2 = (foo, bar) => { return "hello"; }
var b3: (name: string, num: number, boo: boolean) => void;
b3 = (name, number) => { };
var b4: (n: E) => string = (number = 1) => { return "hello"; };
var b5: (n: {}) => string = (number = "string") => { return "hello"; };
// 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);
var b7: ((s: string, w: boolean) => void) | ((s: string, w: number) => string);
b6 = (k) => { k.toLowerCase() };
b6 = (i) => {
i.toExponential();
return i;
}; // 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)
class C<T, U> {
constructor() {
var k: ((j: T, k: U) => (T|U)[]) | ((j: number,k :U) => number[]) = (j, k) => {
return [j, k];
} // Per spec, no contextual signature can be extracted in this case.
}
}