Skip to content

Conformance test for update spec section 4.9.3 contextual typing in function expression #2761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions tests/baselines/reference/functionExpressionContextualTyping1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//// [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 }

// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioning "per spec" doesn't really buy much since this is a conformance test - it should be per spec. Again, mentioning what part of the spec it refers to is more helpful.

}
}

//// [functionExpressionContextualTyping1.js]
// 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
var E;
(function (E) {
E[E["red"] = 0] = "red";
E[E["blue"] = 1] = "blue";
})(E || (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 = function (num, str) {
num.toExponential();
return 0;
};
var Class = (function () {
function Class() {
}
Class.prototype.foo = function () { };
return Class;
})();
var a1 = function (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;
b1 = function (k, h) { };
var b2;
b2 = function (foo, bar) { return foo + 1; };
b2 = function (foo, bar) { return "hello"; };
var b3;
b3 = function (name, number) { };
var b4 = function (number) {
if (number === void 0) { number = 1; }
return "hello";
};
var b5 = function (number) {
if (number === void 0) { 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;
var b7;
b6 = function (k) { k.toLowerCase(); };
b6 = function (i) {
i.toExponential();
return i;
}; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
b7 = function (j, m) { }; // Per spec, no contextual signature can be extracted in this case. (Otherwise clause)
var C = (function () {
function C() {
var k = function (j, k) {
return [j, k];
}; // Per spec, no contextual signature can be extracted in this case.
}
return C;
})();
169 changes: 169 additions & 0 deletions tests/baselines/reference/functionExpressionContextualTyping1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
=== 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 : Symbol(E, Decl(functionExpressionContextualTyping1.ts, 0, 0))
>red : Symbol(E.red, Decl(functionExpressionContextualTyping1.ts, 3, 8))
>blue : Symbol(E.blue, Decl(functionExpressionContextualTyping1.ts, 3, 13))

// 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 : Symbol(a0, Decl(functionExpressionContextualTyping1.ts, 8, 3))
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 8, 9))
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 8, 19))
>num : Symbol(num, Decl(functionExpressionContextualTyping1.ts, 8, 44))
>str : Symbol(str, Decl(functionExpressionContextualTyping1.ts, 8, 48))

num.toExponential();
>num.toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, 469, 45))
>num : Symbol(num, Decl(functionExpressionContextualTyping1.ts, 8, 44))
>toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, 469, 45))

return 0;
}

class Class<T> {
>Class : Symbol(Class, Decl(functionExpressionContextualTyping1.ts, 11, 1))
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 13, 12))

foo() { }
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 13, 16))
}

var a1: (c: Class<Number>) => number = (a1) => {
>a1 : Symbol(a1, Decl(functionExpressionContextualTyping1.ts, 17, 3))
>c : Symbol(c, Decl(functionExpressionContextualTyping1.ts, 17, 9))
>Class : Symbol(Class, Decl(functionExpressionContextualTyping1.ts, 11, 1))
>Number : Symbol(Number, Decl(lib.d.ts, 456, 40), Decl(lib.d.ts, 518, 11))
>a1 : Symbol(a1, Decl(functionExpressionContextualTyping1.ts, 17, 40))

a1.foo();
>a1.foo : Symbol(Class.foo, Decl(functionExpressionContextualTyping1.ts, 13, 16))
>a1 : Symbol(a1, Decl(functionExpressionContextualTyping1.ts, 17, 40))
>foo : Symbol(Class.foo, Decl(functionExpressionContextualTyping1.ts, 13, 16))

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 : Symbol(b1, Decl(functionExpressionContextualTyping1.ts, 27, 3))
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 27, 10))
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 27, 20))
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 27, 46))
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 27, 56))

b1 = (k, h) => { };
>b1 : Symbol(b1, Decl(functionExpressionContextualTyping1.ts, 27, 3))
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 28, 6))
>h : Symbol(h, Decl(functionExpressionContextualTyping1.ts, 28, 8))

var b2: typeof a0 | ((n: number, s: string) => string);
>b2 : Symbol(b2, Decl(functionExpressionContextualTyping1.ts, 29, 3))
>a0 : Symbol(a0, Decl(functionExpressionContextualTyping1.ts, 8, 3))
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 29, 22))
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 29, 32))

b2 = (foo, bar) => { return foo + 1; }
>b2 : Symbol(b2, Decl(functionExpressionContextualTyping1.ts, 29, 3))
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 30, 6))
>bar : Symbol(bar, Decl(functionExpressionContextualTyping1.ts, 30, 10))
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 30, 6))

b2 = (foo, bar) => { return "hello"; }
>b2 : Symbol(b2, Decl(functionExpressionContextualTyping1.ts, 29, 3))
>foo : Symbol(foo, Decl(functionExpressionContextualTyping1.ts, 31, 6))
>bar : Symbol(bar, Decl(functionExpressionContextualTyping1.ts, 31, 10))

var b3: (name: string, num: number, boo: boolean) => void;
>b3 : Symbol(b3, Decl(functionExpressionContextualTyping1.ts, 32, 3))
>name : Symbol(name, Decl(functionExpressionContextualTyping1.ts, 32, 9))
>num : Symbol(num, Decl(functionExpressionContextualTyping1.ts, 32, 22))
>boo : Symbol(boo, Decl(functionExpressionContextualTyping1.ts, 32, 35))

b3 = (name, number) => { };
>b3 : Symbol(b3, Decl(functionExpressionContextualTyping1.ts, 32, 3))
>name : Symbol(name, Decl(functionExpressionContextualTyping1.ts, 33, 6))
>number : Symbol(number, Decl(functionExpressionContextualTyping1.ts, 33, 11))

var b4: (n: E) => string = (number = 1) => { return "hello"; };
>b4 : Symbol(b4, Decl(functionExpressionContextualTyping1.ts, 35, 3))
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 35, 9))
>E : Symbol(E, Decl(functionExpressionContextualTyping1.ts, 0, 0))
>number : Symbol(number, Decl(functionExpressionContextualTyping1.ts, 35, 28))

var b5: (n: {}) => string = (number = "string") => { return "hello"; };
>b5 : Symbol(b5, Decl(functionExpressionContextualTyping1.ts, 36, 3))
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 36, 9))
>number : Symbol(number, Decl(functionExpressionContextualTyping1.ts, 36, 29))

// 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 : Symbol(b6, Decl(functionExpressionContextualTyping1.ts, 40, 3))
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 40, 10))
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 40, 20))
>n : Symbol(n, Decl(functionExpressionContextualTyping1.ts, 40, 46))

var b7: ((s: string, w: boolean) => void) | ((s: string, w: number) => string);
>b7 : Symbol(b7, Decl(functionExpressionContextualTyping1.ts, 41, 3))
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 41, 10))
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 41, 20))
>s : Symbol(s, Decl(functionExpressionContextualTyping1.ts, 41, 46))
>w : Symbol(w, Decl(functionExpressionContextualTyping1.ts, 41, 56))

b6 = (k) => { k.toLowerCase() };
>b6 : Symbol(b6, Decl(functionExpressionContextualTyping1.ts, 40, 3))
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 42, 6))
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 42, 6))

b6 = (i) => {
>b6 : Symbol(b6, Decl(functionExpressionContextualTyping1.ts, 40, 3))
>i : Symbol(i, Decl(functionExpressionContextualTyping1.ts, 43, 6))

i.toExponential();
>i : Symbol(i, Decl(functionExpressionContextualTyping1.ts, 43, 6))

return i;
>i : Symbol(i, Decl(functionExpressionContextualTyping1.ts, 43, 6))

}; // 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 : Symbol(b7, Decl(functionExpressionContextualTyping1.ts, 41, 3))
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 47, 6))
>m : Symbol(m, Decl(functionExpressionContextualTyping1.ts, 47, 8))

class C<T, U> {
>C : Symbol(C, Decl(functionExpressionContextualTyping1.ts, 47, 19))
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 49, 8))
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))

constructor() {
var k: ((j: T, k: U) => (T|U)[]) | ((j: number,k :U) => number[]) = (j, k) => {
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 11))
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 17))
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 49, 8))
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 22))
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))
>T : Symbol(T, Decl(functionExpressionContextualTyping1.ts, 49, 8))
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 45))
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 55))
>U : Symbol(U, Decl(functionExpressionContextualTyping1.ts, 49, 10))
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 77))
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 79))

return [j, k];
>j : Symbol(j, Decl(functionExpressionContextualTyping1.ts, 51, 77))
>k : Symbol(k, Decl(functionExpressionContextualTyping1.ts, 51, 79))

} // Per spec, no contextual signature can be extracted in this case.
}
}
Loading