File: functionExpressionContextualTyping1.js

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (120 lines) | stat: -rw-r--r-- 4,827 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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.
    }
}

//// [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 = /** @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 = /** @class */ (function () {
    function C() {
        var k = function (j, k) {
            return [j, k];
        }; // Per spec, no contextual signature can be extracted in this case.
    }
    return C;
}());