File: contextualTypeWithUnionTypeIndexSignatures.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 (80 lines) | stat: -rw-r--r-- 4,948 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
//// [contextualTypeWithUnionTypeIndexSignatures.ts]
//When used as a contextual type, a union type U has those members that are present in any of 
// its constituent types, with types that are unions of the respective members in the constituent types. 
interface SomeType {
    (a: number): number;
}
interface SomeType2 {
    (a: number): string;
}

interface IWithNoStringIndexSignature {
    foo: string;
}
interface IWithNoNumberIndexSignature {
    0: string;
}
interface IWithStringIndexSignature1 {
    [a: string]: SomeType;
}
interface IWithStringIndexSignature2 {
    [a: string]: SomeType2;
}
interface IWithNumberIndexSignature1 {
    [a: number]: SomeType;
}
interface IWithNumberIndexSignature2 {
    [a: number]: SomeType2;
}

// When an object literal is contextually typed by a type that includes a string index signature, 
// the resulting type of the object literal includes a string index signature with the union type of 
// the types of the properties declared in the object literal, or the Undefined type if the object literal 
// is empty.Likewise, when an object literal is contextually typed by a type that includes a numeric index 
// signature, the resulting type of the object literal includes a numeric index signature with the union type
// of the types of the numerically named properties(section 3.7.4) declared in the object literal, 
// or the Undefined type if the object literal declares no numerically named properties.

// Let S be the set of types in U that has a string index signature.
// If S is not empty, U has a string index signature of a union type of 
// the types of the string index signatures from each type in S.
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; // a should be number
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be number (because of index signature of IWithStringIndexSignature1)
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" }; 
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number


// Let S be the set of types in U that has a numeric index signature.
// If S is not empty, U has a numeric index signature of a union type of 
// the types of the numeric index signatures from each type in S.
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }; // a should be number
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be number (because of index signature of IWithNumberIndexSignature1)
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number

//// [contextualTypeWithUnionTypeIndexSignatures.js]
// When an object literal is contextually typed by a type that includes a string index signature, 
// the resulting type of the object literal includes a string index signature with the union type of 
// the types of the properties declared in the object literal, or the Undefined type if the object literal 
// is empty.Likewise, when an object literal is contextually typed by a type that includes a numeric index 
// signature, the resulting type of the object literal includes a numeric index signature with the union type
// of the types of the numerically named properties(section 3.7.4) declared in the object literal, 
// or the Undefined type if the object literal declares no numerically named properties.
// Let S be the set of types in U that has a string index signature.
// If S is not empty, U has a string index signature of a union type of 
// the types of the string index signatures from each type in S.
var x = { z: function (a) { return a; } }; // a should be number
var x = { foo: function (a) { return a; } }; // a should be number (because of index signature of IWithStringIndexSignature1)
var x = { foo: "hello" };
var x2 = { z: function (a) { return a.toString(); } }; // a should be number
var x2 = { z: function (a) { return a; } }; // a should be number
// Let S be the set of types in U that has a numeric index signature.
// If S is not empty, U has a numeric index signature of a union type of 
// the types of the numeric index signatures from each type in S.
var x3 = { 1: function (a) { return a; } }; // a should be number
var x3 = { 0: function (a) { return a; } }; // a should be number (because of index signature of IWithNumberIndexSignature1)
var x3 = { 0: "hello" };
var x4 = { 1: function (a) { return a.toString(); } }; // a should be number
var x4 = { 1: function (a) { return a; } }; // a should be number