mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
Merge pull request #1058 from Microsoft/contextualUnionTypeConformance
Contextual union type conformance test cases and Fix for contextual union signature
This commit is contained in:
@@ -4890,8 +4890,9 @@ module ts {
|
||||
|
||||
// Return the contextual signature for a given expression node. A contextual type provides a
|
||||
// contextual signature if it has a single call signature and if that call signature is non-generic.
|
||||
// If the contextual type is a union type and each constituent type that has a contextual signature
|
||||
// provides the same contextual signature, then the union type provides that contextual signature.
|
||||
// If the contextual type is a union type, get the signature from each type possible and if they are
|
||||
// all identical ignoring their return type, the result is same signature but with return type as
|
||||
// union type of return types from these signatures
|
||||
function getContextualSignature(node: Expression): Signature {
|
||||
var type = getContextualType(node);
|
||||
if (!type) {
|
||||
@@ -4900,19 +4901,41 @@ module ts {
|
||||
if (!(type.flags & TypeFlags.Union)) {
|
||||
return getNonGenericSignature(type);
|
||||
}
|
||||
var result: Signature;
|
||||
var signatureList: Signature[];
|
||||
var types = (<UnionType>type).types;
|
||||
for (var i = 0; i < types.length; i++) {
|
||||
// The signature set of all constituent type with call signatures should match
|
||||
// So number of signatures allowed is either 0 or 1
|
||||
if (signatureList &&
|
||||
getSignaturesOfObjectOrUnionType(types[i], SignatureKind.Call).length > 1) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var signature = getNonGenericSignature(types[i]);
|
||||
if (signature) {
|
||||
if (!result) {
|
||||
result = signature;
|
||||
if (!signatureList) {
|
||||
// This signature will contribute to contextual union signature
|
||||
signatureList = [signature];
|
||||
}
|
||||
else if (!compareSignatures(result, signature, /*compareReturnTypes*/ true, compareTypes)) {
|
||||
else if (!compareSignatures(signatureList[0], signature, /*compareReturnTypes*/ false, compareTypes)) {
|
||||
// Signatures arent identical, do not use
|
||||
return undefined;
|
||||
}
|
||||
else {
|
||||
// Use this signature for contextual union signature
|
||||
signatureList.push(signature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Result is union of signatures collected (return type is union of return types of this signature set)
|
||||
var result: Signature;
|
||||
if (signatureList) {
|
||||
result = cloneSignature(signatureList[0]);
|
||||
// Clear resolved return type we possibly got from cloneSignature
|
||||
result.resolvedReturnType = undefined;
|
||||
result.unionSignatures = signatureList;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
//// [contextualTypeWithUnionTypeCallSignatures.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.
|
||||
|
||||
// Let S be the set of types in U that have call signatures.
|
||||
// If S is not empty and the sets of call signatures of the types in S are identical ignoring return types,
|
||||
// U has the same set of call signatures, but with return types that are unions of the return types of the respective call signatures from each type in S.
|
||||
|
||||
interface IWithNoCallSignatures {
|
||||
foo: string;
|
||||
}
|
||||
interface IWithCallSignatures {
|
||||
(a: number): string;
|
||||
}
|
||||
interface IWithCallSignatures2 {
|
||||
(a: number): number;
|
||||
}
|
||||
interface IWithCallSignatures3 {
|
||||
(b: string): number;
|
||||
}
|
||||
interface IWithCallSignatures4 {
|
||||
(a: number): string;
|
||||
(a: string, b: number): number;
|
||||
}
|
||||
|
||||
// With no call signature | callSignatures
|
||||
var x: IWithNoCallSignatures | IWithCallSignatures = a => a.toString();
|
||||
|
||||
// With call signatures with different return type
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a.toString(); // Like iWithCallSignatures
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a; // Like iWithCallSignatures2
|
||||
|
||||
// With call signatures of mismatching parameter type
|
||||
var x3: IWithCallSignatures | IWithCallSignatures3 = a => /*here a should be any*/ a.toString();
|
||||
|
||||
// With call signature count mismatch
|
||||
var x4: IWithCallSignatures | IWithCallSignatures4 = a => /*here a should be any*/ a.toString();
|
||||
|
||||
//// [contextualTypeWithUnionTypeCallSignatures.js]
|
||||
//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.
|
||||
// With no call signature | callSignatures
|
||||
var x = function (a) { return a.toString(); };
|
||||
// With call signatures with different return type
|
||||
var x2 = function (a) { return a.toString(); }; // Like iWithCallSignatures
|
||||
var x2 = function (a) { return a; }; // Like iWithCallSignatures2
|
||||
// With call signatures of mismatching parameter type
|
||||
var x3 = function (a /*here a should be any*/) { return a.toString(); };
|
||||
// With call signature count mismatch
|
||||
var x4 = function (a //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.
|
||||
|
||||
// Let S be the set of types in U that have call signatures.
|
||||
// If S is not empty and the sets of call signatures of the types in S are identical ignoring return types,
|
||||
// U has the same set of call signatures, but with return types that are unions of the return types of the respective call signatures from each type in S.
|
||||
|
||||
interface IWithNoCallSignatures {
|
||||
foo: string;
|
||||
}
|
||||
interface IWithCallSignatures {
|
||||
(a: number): string;
|
||||
}
|
||||
interface IWithCallSignatures2 {
|
||||
(a: number): number;
|
||||
}
|
||||
interface IWithCallSignatures3 {
|
||||
(b: string): number;
|
||||
}
|
||||
interface IWithCallSignatures4 {
|
||||
(a: number): string;
|
||||
(a: string, b: number): number;
|
||||
}
|
||||
|
||||
// With no call signature | callSignatures
|
||||
var x: IWithNoCallSignatures | IWithCallSignatures = a => a.toString();
|
||||
|
||||
// With call signatures with different return type
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a.toString(); // Like iWithCallSignatures
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a; // Like iWithCallSignatures2
|
||||
|
||||
// With call signatures of mismatching parameter type
|
||||
var x3: IWithCallSignatures | IWithCallSignatures3 = a => /*here a should be any*/ a.toString();
|
||||
|
||||
// With call signature count mismatch
|
||||
var x4: IWithCallSignatures | IWithCallSignatures4 = a =>
|
||||
) { return a.toString(); };
|
||||
@@ -0,0 +1,99 @@
|
||||
=== tests/cases/conformance/types/union/contextualTypeWithUnionTypeCallSignatures.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.
|
||||
|
||||
// Let S be the set of types in U that have call signatures.
|
||||
// If S is not empty and the sets of call signatures of the types in S are identical ignoring return types,
|
||||
// U has the same set of call signatures, but with return types that are unions of the return types of the respective call signatures from each type in S.
|
||||
|
||||
interface IWithNoCallSignatures {
|
||||
>IWithNoCallSignatures : IWithNoCallSignatures
|
||||
|
||||
foo: string;
|
||||
>foo : string
|
||||
}
|
||||
interface IWithCallSignatures {
|
||||
>IWithCallSignatures : IWithCallSignatures
|
||||
|
||||
(a: number): string;
|
||||
>a : number
|
||||
}
|
||||
interface IWithCallSignatures2 {
|
||||
>IWithCallSignatures2 : IWithCallSignatures2
|
||||
|
||||
(a: number): number;
|
||||
>a : number
|
||||
}
|
||||
interface IWithCallSignatures3 {
|
||||
>IWithCallSignatures3 : IWithCallSignatures3
|
||||
|
||||
(b: string): number;
|
||||
>b : string
|
||||
}
|
||||
interface IWithCallSignatures4 {
|
||||
>IWithCallSignatures4 : IWithCallSignatures4
|
||||
|
||||
(a: number): string;
|
||||
>a : number
|
||||
|
||||
(a: string, b: number): number;
|
||||
>a : string
|
||||
>b : number
|
||||
}
|
||||
|
||||
// With no call signature | callSignatures
|
||||
var x: IWithNoCallSignatures | IWithCallSignatures = a => a.toString();
|
||||
>x : IWithNoCallSignatures | IWithCallSignatures
|
||||
>IWithNoCallSignatures : IWithNoCallSignatures
|
||||
>IWithCallSignatures : IWithCallSignatures
|
||||
>a => a.toString() : (a: number) => string
|
||||
>a : number
|
||||
>a.toString() : string
|
||||
>a.toString : (radix?: number) => string
|
||||
>a : number
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
// With call signatures with different return type
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a.toString(); // Like iWithCallSignatures
|
||||
>x2 : IWithCallSignatures | IWithCallSignatures2
|
||||
>IWithCallSignatures : IWithCallSignatures
|
||||
>IWithCallSignatures2 : IWithCallSignatures2
|
||||
>a => a.toString() : (a: number) => string
|
||||
>a : number
|
||||
>a.toString() : string
|
||||
>a.toString : (radix?: number) => string
|
||||
>a : number
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a; // Like iWithCallSignatures2
|
||||
>x2 : IWithCallSignatures | IWithCallSignatures2
|
||||
>IWithCallSignatures : IWithCallSignatures
|
||||
>IWithCallSignatures2 : IWithCallSignatures2
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
// With call signatures of mismatching parameter type
|
||||
var x3: IWithCallSignatures | IWithCallSignatures3 = a => /*here a should be any*/ a.toString();
|
||||
>x3 : IWithCallSignatures | IWithCallSignatures3
|
||||
>IWithCallSignatures : IWithCallSignatures
|
||||
>IWithCallSignatures3 : IWithCallSignatures3
|
||||
>a => /*here a should be any*/ a.toString() : (a: any) => any
|
||||
>a : any
|
||||
>a.toString() : any
|
||||
>a.toString : any
|
||||
>a : any
|
||||
>toString : any
|
||||
|
||||
// With call signature count mismatch
|
||||
var x4: IWithCallSignatures | IWithCallSignatures4 = a => /*here a should be any*/ a.toString();
|
||||
>x4 : IWithCallSignatures | IWithCallSignatures4
|
||||
>IWithCallSignatures : IWithCallSignatures
|
||||
>IWithCallSignatures4 : IWithCallSignatures4
|
||||
>a => /*here a should be any*/ a.toString() : (a: any) => any
|
||||
>a : any
|
||||
>a.toString() : any
|
||||
>a.toString : any
|
||||
>a : any
|
||||
>toString : any
|
||||
|
||||
@@ -0,0 +1,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 any
|
||||
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 any
|
||||
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 any
|
||||
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 any
|
||||
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
|
||||
@@ -0,0 +1,166 @@
|
||||
=== tests/cases/conformance/types/union/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 {
|
||||
>SomeType : SomeType
|
||||
|
||||
(a: number): number;
|
||||
>a : number
|
||||
}
|
||||
interface SomeType2 {
|
||||
>SomeType2 : SomeType2
|
||||
|
||||
(a: number): string;
|
||||
>a : number
|
||||
}
|
||||
|
||||
interface IWithNoStringIndexSignature {
|
||||
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
|
||||
|
||||
foo: string;
|
||||
>foo : string
|
||||
}
|
||||
interface IWithNoNumberIndexSignature {
|
||||
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
|
||||
|
||||
0: string;
|
||||
}
|
||||
interface IWithStringIndexSignature1 {
|
||||
>IWithStringIndexSignature1 : IWithStringIndexSignature1
|
||||
|
||||
[a: string]: SomeType;
|
||||
>a : string
|
||||
>SomeType : SomeType
|
||||
}
|
||||
interface IWithStringIndexSignature2 {
|
||||
>IWithStringIndexSignature2 : IWithStringIndexSignature2
|
||||
|
||||
[a: string]: SomeType2;
|
||||
>a : string
|
||||
>SomeType2 : SomeType2
|
||||
}
|
||||
interface IWithNumberIndexSignature1 {
|
||||
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
|
||||
|
||||
[a: number]: SomeType;
|
||||
>a : number
|
||||
>SomeType : SomeType
|
||||
}
|
||||
interface IWithNumberIndexSignature2 {
|
||||
>IWithNumberIndexSignature2 : IWithNumberIndexSignature2
|
||||
|
||||
[a: number]: SomeType2;
|
||||
>a : number
|
||||
>SomeType2 : 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
|
||||
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
|
||||
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
|
||||
>IWithStringIndexSignature1 : IWithStringIndexSignature1
|
||||
>{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; }
|
||||
>z : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
|
||||
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
|
||||
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
|
||||
>IWithStringIndexSignature1 : IWithStringIndexSignature1
|
||||
>{ foo: a => a } : { [x: string]: (a: any) => any; foo: (a: any) => any; }
|
||||
>foo : (a: any) => any
|
||||
>a => a : (a: any) => any
|
||||
>a : any
|
||||
>a : any
|
||||
|
||||
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" };
|
||||
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
|
||||
>IWithNoStringIndexSignature : IWithNoStringIndexSignature
|
||||
>IWithStringIndexSignature1 : IWithStringIndexSignature1
|
||||
>{ foo: "hello" } : { [x: string]: string; foo: string; }
|
||||
>foo : string
|
||||
|
||||
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number
|
||||
>x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2
|
||||
>IWithStringIndexSignature1 : IWithStringIndexSignature1
|
||||
>IWithStringIndexSignature2 : IWithStringIndexSignature2
|
||||
>{ z: a => a.toString() } : { [x: string]: (a: number) => string; z: (a: number) => string; }
|
||||
>z : (a: number) => string
|
||||
>a => a.toString() : (a: number) => string
|
||||
>a : number
|
||||
>a.toString() : string
|
||||
>a.toString : (radix?: number) => string
|
||||
>a : number
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number
|
||||
>x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2
|
||||
>IWithStringIndexSignature1 : IWithStringIndexSignature1
|
||||
>IWithStringIndexSignature2 : IWithStringIndexSignature2
|
||||
>{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; }
|
||||
>z : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : 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
|
||||
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
|
||||
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
|
||||
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
|
||||
>{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; }
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
|
||||
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
|
||||
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
|
||||
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
|
||||
>{ 0: a => a } : { [x: number]: (a: any) => any; 0: (a: any) => any; }
|
||||
>a => a : (a: any) => any
|
||||
>a : any
|
||||
>a : any
|
||||
|
||||
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
|
||||
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
|
||||
>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature
|
||||
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
|
||||
>{ 0: "hello" } : { [x: number]: string; 0: string; }
|
||||
|
||||
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number
|
||||
>x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2
|
||||
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
|
||||
>IWithNumberIndexSignature2 : IWithNumberIndexSignature2
|
||||
>{ 1: a => a.toString() } : { [x: number]: (a: number) => string; 1: (a: number) => string; }
|
||||
>a => a.toString() : (a: number) => string
|
||||
>a : number
|
||||
>a.toString() : string
|
||||
>a.toString : (radix?: number) => string
|
||||
>a : number
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number
|
||||
>x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2
|
||||
>IWithNumberIndexSignature1 : IWithNumberIndexSignature1
|
||||
>IWithNumberIndexSignature2 : IWithNumberIndexSignature2
|
||||
>{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; }
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
207
tests/baselines/reference/contextualTypeWithUnionTypeMembers.js
Normal file
207
tests/baselines/reference/contextualTypeWithUnionTypeMembers.js
Normal file
@@ -0,0 +1,207 @@
|
||||
//// [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
|
||||
}];
|
||||
@@ -0,0 +1,438 @@
|
||||
=== tests/cases/conformance/types/union/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> {
|
||||
>I1 : I1<T>
|
||||
>T : T
|
||||
|
||||
commonMethodType(a: string): string;
|
||||
>commonMethodType : (a: string) => string
|
||||
>a : string
|
||||
|
||||
commonPropertyType: string;
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodWithTypeParameter(a: T): T;
|
||||
>commonMethodWithTypeParameter : (a: T) => T
|
||||
>a : T
|
||||
>T : T
|
||||
>T : T
|
||||
|
||||
methodOnlyInI1(a: string): string;
|
||||
>methodOnlyInI1 : (a: string) => string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI1: string;
|
||||
>propertyOnlyInI1 : string
|
||||
}
|
||||
interface I2<T> {
|
||||
>I2 : I2<T>
|
||||
>T : T
|
||||
|
||||
commonMethodType(a: string): string;
|
||||
>commonMethodType : (a: string) => string
|
||||
>a : string
|
||||
|
||||
commonPropertyType: string;
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodWithTypeParameter(a: T): T;
|
||||
>commonMethodWithTypeParameter : (a: T) => T
|
||||
>a : T
|
||||
>T : T
|
||||
>T : T
|
||||
|
||||
methodOnlyInI2(a: string): string;
|
||||
>methodOnlyInI2 : (a: string) => string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI2: 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>;
|
||||
>i1 : I1<number>
|
||||
>I1 : I1<T>
|
||||
|
||||
var i2: I2<number>;
|
||||
>i2 : I2<number>
|
||||
>I2 : I2<T>
|
||||
|
||||
var i1Ori2: I1<number> | I2<number> = i1;
|
||||
>i1Ori2 : I1<number> | I2<number>
|
||||
>I1 : I1<T>
|
||||
>I2 : I2<T>
|
||||
>i1 : I1<number>
|
||||
|
||||
var i1Ori2: I1<number> | I2<number> = i2;
|
||||
>i1Ori2 : I1<number> | I2<number>
|
||||
>I1 : I1<T>
|
||||
>I2 : I2<T>
|
||||
>i2 : I2<number>
|
||||
|
||||
var i1Ori2: I1<number> | I2<number> = { // Like i1
|
||||
>i1Ori2 : I1<number> | I2<number>
|
||||
>I1 : I1<T>
|
||||
>I2 : I2<T>
|
||||
>{ // Like i1 commonPropertyType: "hello", commonMethodType: a=> a, commonMethodWithTypeParameter: a => a, methodOnlyInI1: a => a, propertyOnlyInI1: "Hello",} : { commonPropertyType: string; commonMethodType: (a: string) => string; commonMethodWithTypeParameter: (a: number) => number; methodOnlyInI1: (a: string) => string; propertyOnlyInI1: string; }
|
||||
|
||||
commonPropertyType: "hello",
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodType: a=> a,
|
||||
>commonMethodType : (a: string) => string
|
||||
>a=> a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
commonMethodWithTypeParameter: a => a,
|
||||
>commonMethodWithTypeParameter : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
methodOnlyInI1: a => a,
|
||||
>methodOnlyInI1 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI1: "Hello",
|
||||
>propertyOnlyInI1 : string
|
||||
|
||||
};
|
||||
var i1Ori2: I1<number> | I2<number> = { // Like i2
|
||||
>i1Ori2 : I1<number> | I2<number>
|
||||
>I1 : I1<T>
|
||||
>I2 : I2<T>
|
||||
>{ // Like i2 commonPropertyType: "hello", commonMethodType: a=> a, commonMethodWithTypeParameter: a => a, methodOnlyInI2: a => a, propertyOnlyInI2: "Hello",} : { commonPropertyType: string; commonMethodType: (a: string) => string; commonMethodWithTypeParameter: (a: number) => number; methodOnlyInI2: (a: string) => string; propertyOnlyInI2: string; }
|
||||
|
||||
commonPropertyType: "hello",
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodType: a=> a,
|
||||
>commonMethodType : (a: string) => string
|
||||
>a=> a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
commonMethodWithTypeParameter: a => a,
|
||||
>commonMethodWithTypeParameter : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
methodOnlyInI2: a => a,
|
||||
>methodOnlyInI2 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI2: "Hello",
|
||||
>propertyOnlyInI2 : string
|
||||
|
||||
};
|
||||
var i1Ori2: I1<number> | I2<number> = { // Like i1 and i2 both
|
||||
>i1Ori2 : I1<number> | I2<number>
|
||||
>I1 : I1<T>
|
||||
>I2 : I2<T>
|
||||
>{ // Like i1 and i2 both commonPropertyType: "hello", commonMethodType: a=> a, commonMethodWithTypeParameter: a => a, methodOnlyInI1: a => a, propertyOnlyInI1: "Hello", methodOnlyInI2: a => a, propertyOnlyInI2: "Hello",} : { commonPropertyType: string; commonMethodType: (a: string) => string; commonMethodWithTypeParameter: (a: number) => number; methodOnlyInI1: (a: string) => string; propertyOnlyInI1: string; methodOnlyInI2: (a: string) => string; propertyOnlyInI2: string; }
|
||||
|
||||
commonPropertyType: "hello",
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodType: a=> a,
|
||||
>commonMethodType : (a: string) => string
|
||||
>a=> a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
commonMethodWithTypeParameter: a => a,
|
||||
>commonMethodWithTypeParameter : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
methodOnlyInI1: a => a,
|
||||
>methodOnlyInI1 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI1: "Hello",
|
||||
>propertyOnlyInI1 : string
|
||||
|
||||
methodOnlyInI2: a => a,
|
||||
>methodOnlyInI2 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI2: "Hello",
|
||||
>propertyOnlyInI2 : string
|
||||
|
||||
};
|
||||
|
||||
var arrayI1OrI2: Array<I1<number> | I2<number>> = [i1, i2, { // Like i1
|
||||
>arrayI1OrI2 : (I1<number> | I2<number>)[]
|
||||
>Array : T[]
|
||||
>I1 : I1<T>
|
||||
>I2 : I2<T>
|
||||
>[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", }] : (I1<number> | I2<number>)[]
|
||||
>i1 : I1<number>
|
||||
>i2 : I2<number>
|
||||
>{ // Like i1 commonPropertyType: "hello", commonMethodType: a=> a, commonMethodWithTypeParameter: a => a, methodOnlyInI1: a => a, propertyOnlyInI1: "Hello", } : { commonPropertyType: string; commonMethodType: (a: string) => string; commonMethodWithTypeParameter: (a: number) => number; methodOnlyInI1: (a: string) => string; propertyOnlyInI1: string; }
|
||||
|
||||
commonPropertyType: "hello",
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodType: a=> a,
|
||||
>commonMethodType : (a: string) => string
|
||||
>a=> a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
commonMethodWithTypeParameter: a => a,
|
||||
>commonMethodWithTypeParameter : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
methodOnlyInI1: a => a,
|
||||
>methodOnlyInI1 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI1: "Hello",
|
||||
>propertyOnlyInI1 : string
|
||||
|
||||
},
|
||||
{ // Like i2
|
||||
>{ // Like i2 commonPropertyType: "hello", commonMethodType: a=> a, commonMethodWithTypeParameter: a => a, methodOnlyInI2: a => a, propertyOnlyInI2: "Hello", } : { commonPropertyType: string; commonMethodType: (a: string) => string; commonMethodWithTypeParameter: (a: number) => number; methodOnlyInI2: (a: string) => string; propertyOnlyInI2: string; }
|
||||
|
||||
commonPropertyType: "hello",
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodType: a=> a,
|
||||
>commonMethodType : (a: string) => string
|
||||
>a=> a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
commonMethodWithTypeParameter: a => a,
|
||||
>commonMethodWithTypeParameter : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
methodOnlyInI2: a => a,
|
||||
>methodOnlyInI2 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI2: "Hello",
|
||||
>propertyOnlyInI2 : string
|
||||
|
||||
}, { // Like i1 and i2 both
|
||||
>{ // Like i1 and i2 both commonPropertyType: "hello", commonMethodType: a=> a, commonMethodWithTypeParameter: a => a, methodOnlyInI1: a => a, propertyOnlyInI1: "Hello", methodOnlyInI2: a => a, propertyOnlyInI2: "Hello", } : { commonPropertyType: string; commonMethodType: (a: string) => string; commonMethodWithTypeParameter: (a: number) => number; methodOnlyInI1: (a: string) => string; propertyOnlyInI1: string; methodOnlyInI2: (a: string) => string; propertyOnlyInI2: string; }
|
||||
|
||||
commonPropertyType: "hello",
|
||||
>commonPropertyType : string
|
||||
|
||||
commonMethodType: a=> a,
|
||||
>commonMethodType : (a: string) => string
|
||||
>a=> a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
commonMethodWithTypeParameter: a => a,
|
||||
>commonMethodWithTypeParameter : (a: number) => number
|
||||
>a => a : (a: number) => number
|
||||
>a : number
|
||||
>a : number
|
||||
|
||||
methodOnlyInI1: a => a,
|
||||
>methodOnlyInI1 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI1: "Hello",
|
||||
>propertyOnlyInI1 : string
|
||||
|
||||
methodOnlyInI2: a => a,
|
||||
>methodOnlyInI2 : (a: string) => string
|
||||
>a => a : (a: string) => string
|
||||
>a : string
|
||||
>a : string
|
||||
|
||||
propertyOnlyInI2: "Hello",
|
||||
>propertyOnlyInI2 : string
|
||||
|
||||
}];
|
||||
|
||||
interface I11 {
|
||||
>I11 : I11
|
||||
|
||||
commonMethodDifferentReturnType(a: string, b: number): string;
|
||||
>commonMethodDifferentReturnType : (a: string, b: number) => string
|
||||
>a : string
|
||||
>b : number
|
||||
|
||||
commonPropertyDifferentType: string;
|
||||
>commonPropertyDifferentType : string
|
||||
}
|
||||
interface I21 {
|
||||
>I21 : I21
|
||||
|
||||
commonMethodDifferentReturnType(a: string, b: number): number;
|
||||
>commonMethodDifferentReturnType : (a: string, b: number) => number
|
||||
>a : string
|
||||
>b : number
|
||||
|
||||
commonPropertyDifferentType: number;
|
||||
>commonPropertyDifferentType : number
|
||||
}
|
||||
var i11: I11;
|
||||
>i11 : I11
|
||||
>I11 : I11
|
||||
|
||||
var i21: I21;
|
||||
>i21 : I21
|
||||
>I21 : I21
|
||||
|
||||
var i11Ori21: I11 | I21 = i11;
|
||||
>i11Ori21 : I11 | I21
|
||||
>I11 : I11
|
||||
>I21 : I21
|
||||
>i11 : I11
|
||||
|
||||
var i11Ori21: I11 | I21 = i21;
|
||||
>i11Ori21 : I11 | I21
|
||||
>I11 : I11
|
||||
>I21 : I21
|
||||
>i21 : I21
|
||||
|
||||
var i11Ori21: I11 | I21 = {
|
||||
>i11Ori21 : I11 | I21
|
||||
>I11 : I11
|
||||
>I21 : I21
|
||||
>{ // Like i1 commonMethodDifferentReturnType: (a, b) => { var z = a.charAt(b); return z; }, commonPropertyDifferentType: "hello", } : { commonMethodDifferentReturnType: (a: string, b: number) => string; commonPropertyDifferentType: string; }
|
||||
|
||||
// Like i1
|
||||
commonMethodDifferentReturnType: (a, b) => {
|
||||
>commonMethodDifferentReturnType : (a: string, b: number) => string
|
||||
>(a, b) => { var z = a.charAt(b); return z; } : (a: string, b: number) => string
|
||||
>a : string
|
||||
>b : number
|
||||
|
||||
var z = a.charAt(b);
|
||||
>z : string
|
||||
>a.charAt(b) : string
|
||||
>a.charAt : (pos: number) => string
|
||||
>a : string
|
||||
>charAt : (pos: number) => string
|
||||
>b : number
|
||||
|
||||
return z;
|
||||
>z : string
|
||||
|
||||
},
|
||||
commonPropertyDifferentType: "hello",
|
||||
>commonPropertyDifferentType : string
|
||||
|
||||
};
|
||||
var i11Ori21: I11 | I21 = {
|
||||
>i11Ori21 : I11 | I21
|
||||
>I11 : I11
|
||||
>I21 : I21
|
||||
>{ // Like i2 commonMethodDifferentReturnType: (a, b) => { var z = a.charCodeAt(b); return z; }, commonPropertyDifferentType: 10,} : { commonMethodDifferentReturnType: (a: string, b: number) => number; commonPropertyDifferentType: number; }
|
||||
|
||||
// Like i2
|
||||
commonMethodDifferentReturnType: (a, b) => {
|
||||
>commonMethodDifferentReturnType : (a: string, b: number) => number
|
||||
>(a, b) => { var z = a.charCodeAt(b); return z; } : (a: string, b: number) => number
|
||||
>a : string
|
||||
>b : number
|
||||
|
||||
var z = a.charCodeAt(b);
|
||||
>z : number
|
||||
>a.charCodeAt(b) : number
|
||||
>a.charCodeAt : (index: number) => number
|
||||
>a : string
|
||||
>charCodeAt : (index: number) => number
|
||||
>b : number
|
||||
|
||||
return z;
|
||||
>z : number
|
||||
|
||||
},
|
||||
commonPropertyDifferentType: 10,
|
||||
>commonPropertyDifferentType : number
|
||||
|
||||
};
|
||||
var arrayOrI11OrI21: Array<I11 | I21> = [i11, i21, i11 || i21, {
|
||||
>arrayOrI11OrI21 : (I11 | I21)[]
|
||||
>Array : T[]
|
||||
>I11 : I11
|
||||
>I21 : 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, }] : (I11 | I21)[]
|
||||
>i11 : I11
|
||||
>i21 : I21
|
||||
>i11 || i21 : I11 | I21
|
||||
>i11 : I11
|
||||
>i21 : I21
|
||||
>{ // Like i1 commonMethodDifferentReturnType: (a, b) => { var z = a.charAt(b); return z; }, commonPropertyDifferentType: "hello", } : { commonMethodDifferentReturnType: (a: string, b: number) => string; commonPropertyDifferentType: string; }
|
||||
|
||||
// Like i1
|
||||
commonMethodDifferentReturnType: (a, b) => {
|
||||
>commonMethodDifferentReturnType : (a: string, b: number) => string
|
||||
>(a, b) => { var z = a.charAt(b); return z; } : (a: string, b: number) => string
|
||||
>a : string
|
||||
>b : number
|
||||
|
||||
var z = a.charAt(b);
|
||||
>z : string
|
||||
>a.charAt(b) : string
|
||||
>a.charAt : (pos: number) => string
|
||||
>a : string
|
||||
>charAt : (pos: number) => string
|
||||
>b : number
|
||||
|
||||
return z;
|
||||
>z : string
|
||||
|
||||
},
|
||||
commonPropertyDifferentType: "hello",
|
||||
>commonPropertyDifferentType : string
|
||||
|
||||
}, {
|
||||
>{ // Like i2 commonMethodDifferentReturnType: (a, b) => { var z = a.charCodeAt(b); return z; }, commonPropertyDifferentType: 10, } : { commonMethodDifferentReturnType: (a: string, b: number) => number; commonPropertyDifferentType: number; }
|
||||
|
||||
// Like i2
|
||||
commonMethodDifferentReturnType: (a, b) => {
|
||||
>commonMethodDifferentReturnType : (a: string, b: number) => number
|
||||
>(a, b) => { var z = a.charCodeAt(b); return z; } : (a: string, b: number) => number
|
||||
>a : string
|
||||
>b : number
|
||||
|
||||
var z = a.charCodeAt(b);
|
||||
>z : number
|
||||
>a.charCodeAt(b) : number
|
||||
>a.charCodeAt : (index: number) => number
|
||||
>a : string
|
||||
>charCodeAt : (index: number) => number
|
||||
>b : number
|
||||
|
||||
return z;
|
||||
>z : number
|
||||
|
||||
},
|
||||
commonPropertyDifferentType: 10,
|
||||
>commonPropertyDifferentType : number
|
||||
|
||||
}];
|
||||
@@ -0,0 +1,130 @@
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(14,5): error TS2323: Type '{ prop: string | number; }' is not assignable to type '{ prop: string; } | { prop: number; }'.
|
||||
Type '{ prop: string | number; }' is not assignable to type '{ prop: number; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(20,5): error TS2323: Type '{ prop: string | number; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
|
||||
Type '{ prop: string | number; }' is not assignable to type '{ prop: number; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(21,5): error TS2323: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
|
||||
Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(25,5): error TS2323: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
|
||||
Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(29,5): error TS2323: Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
|
||||
Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(57,5): error TS2323: Type '{ commonMethodDifferentReturnType: (a: string, b: number) => string | number; }' is not assignable to type 'I11 | I21'.
|
||||
Type '{ commonMethodDifferentReturnType: (a: string, b: number) => string | number; }' is not assignable to type 'I21'.
|
||||
Types of property 'commonMethodDifferentReturnType' are incompatible.
|
||||
Type '(a: string, b: number) => string | number' is not assignable to type '(a: string, b: number) => number'.
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts (6 errors) ====
|
||||
var str: string;
|
||||
var num: number;
|
||||
var strOrNumber: string | number = str || num;
|
||||
var objStr: { prop: string };
|
||||
var objNum: { prop: number };
|
||||
var objStrOrNum1: { prop: string } | { prop: number } = objStr || objNum;
|
||||
var objStrOrNum2: { prop: string | number } = objStr || objNum;
|
||||
// Below is error because :
|
||||
// Spec says:
|
||||
// S is a union type and each constituent type of S is assignable to T.
|
||||
// T is a union type and S is assignable to at least one constituent type of T.
|
||||
// In case of objStrOrNum3, the S is not union Type but object Literal so we go to next step.
|
||||
// Since T is union Type we only allow the assignment of either object with property of type string or object with property of type number but do not allow object with property of type string | number
|
||||
var objStrOrNum3: { prop: string } | { prop: number } = {
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2323: Type '{ prop: string | number; }' is not assignable to type '{ prop: string; } | { prop: number; }'.
|
||||
!!! error TS2323: Type '{ prop: string | number; }' is not assignable to type '{ prop: number; }'.
|
||||
!!! error TS2323: Types of property 'prop' are incompatible.
|
||||
!!! error TS2323: Type 'string | number' is not assignable to type 'number'.
|
||||
!!! error TS2323: Type 'string' is not assignable to type 'number'.
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum4: { prop: string | number } = {
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum5: { prop: string; anotherP: string; } | { prop: number } = { prop: strOrNumber };
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2323: Type '{ prop: string | number; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
|
||||
!!! error TS2323: Type '{ prop: string | number; }' is not assignable to type '{ prop: number; }'.
|
||||
!!! error TS2323: Types of property 'prop' are incompatible.
|
||||
!!! error TS2323: Type 'string | number' is not assignable to type 'number'.
|
||||
!!! error TS2323: Type 'string' is not assignable to type 'number'.
|
||||
var objStrOrNum6: { prop: string; anotherP: string; } | { prop: number } = {
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2323: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
|
||||
!!! error TS2323: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; }'.
|
||||
!!! error TS2323: Types of property 'prop' are incompatible.
|
||||
!!! error TS2323: Type 'string | number' is not assignable to type 'number'.
|
||||
!!! error TS2323: Type 'string' is not assignable to type 'number'.
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum7: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2323: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
|
||||
!!! error TS2323: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
|
||||
!!! error TS2323: Types of property 'prop' are incompatible.
|
||||
!!! error TS2323: Type 'string | number' is not assignable to type 'number'.
|
||||
!!! error TS2323: Type 'string' is not assignable to type 'number'.
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum8: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2323: Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
|
||||
!!! error TS2323: Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
|
||||
!!! error TS2323: Types of property 'prop' are incompatible.
|
||||
!!! error TS2323: Type 'string | number' is not assignable to type 'number'.
|
||||
!!! error TS2323: Type 'string' is not assignable to type 'number'.
|
||||
prop: strOrNumber,
|
||||
anotherP: str,
|
||||
anotherP1: num
|
||||
};
|
||||
interface I11 {
|
||||
commonMethodDifferentReturnType(a: string, b: number): string;
|
||||
}
|
||||
interface I21 {
|
||||
commonMethodDifferentReturnType(a: string, b: number): 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;
|
||||
},
|
||||
};
|
||||
var i11Ori21: I11 | I21 = { // Like i2
|
||||
commonMethodDifferentReturnType: (a, b) => {
|
||||
var z = a.charCodeAt(b);
|
||||
return z;
|
||||
},
|
||||
};
|
||||
var strOrNumber: string | number;
|
||||
var i11Ori21: I11 | I21 = { // Like i1 and i2 both
|
||||
~~~~~~~~
|
||||
!!! error TS2323: Type '{ commonMethodDifferentReturnType: (a: string, b: number) => string | number; }' is not assignable to type 'I11 | I21'.
|
||||
!!! error TS2323: Type '{ commonMethodDifferentReturnType: (a: string, b: number) => string | number; }' is not assignable to type 'I21'.
|
||||
!!! error TS2323: Types of property 'commonMethodDifferentReturnType' are incompatible.
|
||||
!!! error TS2323: Type '(a: string, b: number) => string | number' is not assignable to type '(a: string, b: number) => number'.
|
||||
!!! error TS2323: Type 'string | number' is not assignable to type 'number'.
|
||||
!!! error TS2323: Type 'string' is not assignable to type 'number'.
|
||||
commonMethodDifferentReturnType: (a, b) => strOrNumber,
|
||||
};
|
||||
@@ -0,0 +1,115 @@
|
||||
//// [contextualTypeWithUnionTypeObjectLiteral.ts]
|
||||
var str: string;
|
||||
var num: number;
|
||||
var strOrNumber: string | number = str || num;
|
||||
var objStr: { prop: string };
|
||||
var objNum: { prop: number };
|
||||
var objStrOrNum1: { prop: string } | { prop: number } = objStr || objNum;
|
||||
var objStrOrNum2: { prop: string | number } = objStr || objNum;
|
||||
// Below is error because :
|
||||
// Spec says:
|
||||
// S is a union type and each constituent type of S is assignable to T.
|
||||
// T is a union type and S is assignable to at least one constituent type of T.
|
||||
// In case of objStrOrNum3, the S is not union Type but object Literal so we go to next step.
|
||||
// Since T is union Type we only allow the assignment of either object with property of type string or object with property of type number but do not allow object with property of type string | number
|
||||
var objStrOrNum3: { prop: string } | { prop: number } = {
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum4: { prop: string | number } = {
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum5: { prop: string; anotherP: string; } | { prop: number } = { prop: strOrNumber };
|
||||
var objStrOrNum6: { prop: string; anotherP: string; } | { prop: number } = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum7: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum8: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str,
|
||||
anotherP1: num
|
||||
};
|
||||
interface I11 {
|
||||
commonMethodDifferentReturnType(a: string, b: number): string;
|
||||
}
|
||||
interface I21 {
|
||||
commonMethodDifferentReturnType(a: string, b: number): 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;
|
||||
},
|
||||
};
|
||||
var i11Ori21: I11 | I21 = { // Like i2
|
||||
commonMethodDifferentReturnType: (a, b) => {
|
||||
var z = a.charCodeAt(b);
|
||||
return z;
|
||||
},
|
||||
};
|
||||
var strOrNumber: string | number;
|
||||
var i11Ori21: I11 | I21 = { // Like i1 and i2 both
|
||||
commonMethodDifferentReturnType: (a, b) => strOrNumber,
|
||||
};
|
||||
|
||||
//// [contextualTypeWithUnionTypeObjectLiteral.js]
|
||||
var str;
|
||||
var num;
|
||||
var strOrNumber = str || num;
|
||||
var objStr;
|
||||
var objNum;
|
||||
var objStrOrNum1 = objStr || objNum;
|
||||
var objStrOrNum2 = objStr || objNum;
|
||||
// Below is error because :
|
||||
// Spec says:
|
||||
// S is a union type and each constituent type of S is assignable to T.
|
||||
// T is a union type and S is assignable to at least one constituent type of T.
|
||||
// In case of objStrOrNum3, the S is not union Type but object Literal so we go to next step.
|
||||
// Since T is union Type we only allow the assignment of either object with property of type string or object with property of type number but do not allow object with property of type string | number
|
||||
var objStrOrNum3 = {
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum4 = {
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum5 = { prop: strOrNumber };
|
||||
var objStrOrNum6 = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum7 = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum8 = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str,
|
||||
anotherP1: num
|
||||
};
|
||||
var i11;
|
||||
var i21;
|
||||
var i11Ori21 = i11;
|
||||
var i11Ori21 = i21;
|
||||
var i11Ori21 = {
|
||||
commonMethodDifferentReturnType: function (a, b) {
|
||||
var z = a.charAt(b);
|
||||
return z;
|
||||
}
|
||||
};
|
||||
var i11Ori21 = {
|
||||
commonMethodDifferentReturnType: function (a, b) {
|
||||
var z = a.charCodeAt(b);
|
||||
return z;
|
||||
}
|
||||
};
|
||||
var strOrNumber;
|
||||
var i11Ori21 = {
|
||||
commonMethodDifferentReturnType: function (a, b) { return strOrNumber; }
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
//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.
|
||||
|
||||
// Let S be the set of types in U that have call signatures.
|
||||
// If S is not empty and the sets of call signatures of the types in S are identical ignoring return types,
|
||||
// U has the same set of call signatures, but with return types that are unions of the return types of the respective call signatures from each type in S.
|
||||
|
||||
interface IWithNoCallSignatures {
|
||||
foo: string;
|
||||
}
|
||||
interface IWithCallSignatures {
|
||||
(a: number): string;
|
||||
}
|
||||
interface IWithCallSignatures2 {
|
||||
(a: number): number;
|
||||
}
|
||||
interface IWithCallSignatures3 {
|
||||
(b: string): number;
|
||||
}
|
||||
interface IWithCallSignatures4 {
|
||||
(a: number): string;
|
||||
(a: string, b: number): number;
|
||||
}
|
||||
|
||||
// With no call signature | callSignatures
|
||||
var x: IWithNoCallSignatures | IWithCallSignatures = a => a.toString();
|
||||
|
||||
// With call signatures with different return type
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a.toString(); // Like iWithCallSignatures
|
||||
var x2: IWithCallSignatures | IWithCallSignatures2 = a => a; // Like iWithCallSignatures2
|
||||
|
||||
// With call signatures of mismatching parameter type
|
||||
var x3: IWithCallSignatures | IWithCallSignatures3 = a => /*here a should be any*/ a.toString();
|
||||
|
||||
// With call signature count mismatch
|
||||
var x4: IWithCallSignatures | IWithCallSignatures4 = a => /*here a should be any*/ a.toString();
|
||||
@@ -0,0 +1,54 @@
|
||||
//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 any
|
||||
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 any
|
||||
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
|
||||
@@ -0,0 +1,119 @@
|
||||
//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,
|
||||
}];
|
||||
@@ -0,0 +1,59 @@
|
||||
var str: string;
|
||||
var num: number;
|
||||
var strOrNumber: string | number = str || num;
|
||||
var objStr: { prop: string };
|
||||
var objNum: { prop: number };
|
||||
var objStrOrNum1: { prop: string } | { prop: number } = objStr || objNum;
|
||||
var objStrOrNum2: { prop: string | number } = objStr || objNum;
|
||||
// Below is error because :
|
||||
// Spec says:
|
||||
// S is a union type and each constituent type of S is assignable to T.
|
||||
// T is a union type and S is assignable to at least one constituent type of T.
|
||||
// In case of objStrOrNum3, the S is not union Type but object Literal so we go to next step.
|
||||
// Since T is union Type we only allow the assignment of either object with property of type string or object with property of type number but do not allow object with property of type string | number
|
||||
var objStrOrNum3: { prop: string } | { prop: number } = {
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum4: { prop: string | number } = {
|
||||
prop: strOrNumber
|
||||
};
|
||||
var objStrOrNum5: { prop: string; anotherP: string; } | { prop: number } = { prop: strOrNumber };
|
||||
var objStrOrNum6: { prop: string; anotherP: string; } | { prop: number } = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum7: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum8: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
|
||||
prop: strOrNumber,
|
||||
anotherP: str,
|
||||
anotherP1: num
|
||||
};
|
||||
interface I11 {
|
||||
commonMethodDifferentReturnType(a: string, b: number): string;
|
||||
}
|
||||
interface I21 {
|
||||
commonMethodDifferentReturnType(a: string, b: number): 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;
|
||||
},
|
||||
};
|
||||
var i11Ori21: I11 | I21 = { // Like i2
|
||||
commonMethodDifferentReturnType: (a, b) => {
|
||||
var z = a.charCodeAt(b);
|
||||
return z;
|
||||
},
|
||||
};
|
||||
var strOrNumber: string | number;
|
||||
var i11Ori21: I11 | I21 = { // Like i1 and i2 both
|
||||
commonMethodDifferentReturnType: (a, b) => strOrNumber,
|
||||
};
|
||||
Reference in New Issue
Block a user