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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
//// [contextualTypeWithUnionTypeMembers.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 I1<T> {
commonMethodType(a: string): string;
commonPropertyType: string;
commonMethodWithTypeParameter(a: T): T;
methodOnlyInI1(a: string): string;
propertyOnlyInI1: string;
}
interface I2<T> {
commonMethodType(a: string): string;
commonPropertyType: string;
commonMethodWithTypeParameter(a: T): T;
methodOnlyInI2(a: string): string;
propertyOnlyInI2: string;
}
// Let S be the set of types in U that has a property P.
// If S is not empty, U has a property P of a union type of the types of P from each type in S.
var i1: I1<number>;
var i2: I2<number>;
var i1Ori2: I1<number> | I2<number> = i1;
var i1Ori2: I1<number> | I2<number> = i2;
var i1Ori2: I1<number> | I2<number> = { // Like i1
commonPropertyType: "hello",
commonMethodType: a=> a,
commonMethodWithTypeParameter: a => a,
methodOnlyInI1: a => a,
propertyOnlyInI1: "Hello",
};
var i1Ori2: I1<number> | I2<number> = { // Like i2
commonPropertyType: "hello",
commonMethodType: a=> a,
commonMethodWithTypeParameter: a => a,
methodOnlyInI2: a => a,
propertyOnlyInI2: "Hello",
};
var i1Ori2: I1<number> | I2<number> = { // Like i1 and i2 both
commonPropertyType: "hello",
commonMethodType: a=> a,
commonMethodWithTypeParameter: a => a,
methodOnlyInI1: a => a,
propertyOnlyInI1: "Hello",
methodOnlyInI2: a => a,
propertyOnlyInI2: "Hello",
};
var arrayI1OrI2: Array<I1<number> | I2<number>> = [i1, i2, { // Like i1
commonPropertyType: "hello",
commonMethodType: a=> a,
commonMethodWithTypeParameter: a => a,
methodOnlyInI1: a => a,
propertyOnlyInI1: "Hello",
},
{ // Like i2
commonPropertyType: "hello",
commonMethodType: a=> a,
commonMethodWithTypeParameter: a => a,
methodOnlyInI2: a => a,
propertyOnlyInI2: "Hello",
}, { // Like i1 and i2 both
commonPropertyType: "hello",
commonMethodType: a=> a,
commonMethodWithTypeParameter: a => a,
methodOnlyInI1: a => a,
propertyOnlyInI1: "Hello",
methodOnlyInI2: a => a,
propertyOnlyInI2: "Hello",
}];
interface I11 {
commonMethodDifferentReturnType(a: string, b: number): string;
commonPropertyDifferentType: string;
}
interface I21 {
commonMethodDifferentReturnType(a: string, b: number): number;
commonPropertyDifferentType: number;
}
var i11: I11;
var i21: I21;
var i11Ori21: I11 | I21 = i11;
var i11Ori21: I11 | I21 = i21;
var i11Ori21: I11 | I21 = {
// Like i1
commonMethodDifferentReturnType: (a, b) => {
var z = a.charAt(b);
return z;
},
commonPropertyDifferentType: "hello",
};
var i11Ori21: I11 | I21 = {
// Like i2
commonMethodDifferentReturnType: (a, b) => {
var z = a.charCodeAt(b);
return z;
},
commonPropertyDifferentType: 10,
};
var arrayOrI11OrI21: Array<I11 | I21> = [i11, i21, i11 || i21, {
// Like i1
commonMethodDifferentReturnType: (a, b) => {
var z = a.charAt(b);
return z;
},
commonPropertyDifferentType: "hello",
}, {
// Like i2
commonMethodDifferentReturnType: (a, b) => {
var z = a.charCodeAt(b);
return z;
},
commonPropertyDifferentType: 10,
}];
//// [contextualTypeWithUnionTypeMembers.js]
// Let S be the set of types in U that has a property P.
// If S is not empty, U has a property P of a union type of the types of P from each type in S.
var i1;
var i2;
var i1Ori2 = i1;
var i1Ori2 = i2;
var i1Ori2 = {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello"
};
var i1Ori2 = {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
};
var i1Ori2 = {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello",
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
};
var arrayI1OrI2 = [i1, i2, {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello"
},
{
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
}, {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello",
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
}];
var i11;
var i21;
var i11Ori21 = i11;
var i11Ori21 = i21;
var i11Ori21 = {
// Like i1
commonMethodDifferentReturnType: function (a, b) {
var z = a.charAt(b);
return z;
},
commonPropertyDifferentType: "hello"
};
var i11Ori21 = {
// Like i2
commonMethodDifferentReturnType: function (a, b) {
var z = a.charCodeAt(b);
return z;
},
commonPropertyDifferentType: 10
};
var arrayOrI11OrI21 = [i11, i21, i11 || i21, {
// Like i1
commonMethodDifferentReturnType: function (a, b) {
var z = a.charAt(b);
return z;
},
commonPropertyDifferentType: "hello"
}, {
// Like i2
commonMethodDifferentReturnType: function (a, b) {
var z = a.charCodeAt(b);
return z;
},
commonPropertyDifferentType: 10
}];
|