From 357bd87612ba2e1f5302562a922f943405ea70b3 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 3 Nov 2014 13:20:30 -0800 Subject: [PATCH] Tests for union members: If each type in U has a property P, U has a property P of a union type of the types of P from each type in U. --- .../reference/unionTypeEquivalence.js | 4 +- .../reference/unionTypeEquivalence.types | 3 +- .../reference/unionTypeMembers.errors.txt | 72 +++++++++++++++++ tests/baselines/reference/unionTypeMembers.js | 79 +++++++++++++++++++ .../types/union/unionTypeEquivalence.ts | 2 +- .../types/union/unionTypeMembers.ts | 54 +++++++++++++ 6 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/unionTypeMembers.errors.txt create mode 100644 tests/baselines/reference/unionTypeMembers.js create mode 100644 tests/cases/conformance/types/union/unionTypeMembers.ts diff --git a/tests/baselines/reference/unionTypeEquivalence.js b/tests/baselines/reference/unionTypeEquivalence.js index 3698ecd2f92..f9fdf2e58d7 100644 --- a/tests/baselines/reference/unionTypeEquivalence.js +++ b/tests/baselines/reference/unionTypeEquivalence.js @@ -1,7 +1,7 @@ //// [unionTypeEquivalence.ts] // A | B is equivalent to A if B is a subtype of A class C { } -class D extends C { } +class D extends C { foo() { } } var x: C; var x : C | D; @@ -37,6 +37,8 @@ var D = (function (_super) { function D() { _super.apply(this, arguments); } + D.prototype.foo = function () { + }; return D; })(C); var x; diff --git a/tests/baselines/reference/unionTypeEquivalence.types b/tests/baselines/reference/unionTypeEquivalence.types index 53e95659120..c4990a58132 100644 --- a/tests/baselines/reference/unionTypeEquivalence.types +++ b/tests/baselines/reference/unionTypeEquivalence.types @@ -3,9 +3,10 @@ class C { } >C : C -class D extends C { } +class D extends C { foo() { } } >D : D >C : C +>foo : () => void var x: C; >x : C diff --git a/tests/baselines/reference/unionTypeMembers.errors.txt b/tests/baselines/reference/unionTypeMembers.errors.txt new file mode 100644 index 00000000000..21f1204ac6d --- /dev/null +++ b/tests/baselines/reference/unionTypeMembers.errors.txt @@ -0,0 +1,72 @@ +tests/cases/conformance/types/union/unionTypeMembers.ts(44,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. +tests/cases/conformance/types/union/unionTypeMembers.ts(51,3): error TS2339: Property 'propertyOnlyInI1' does not exist on type 'I1 | I2'. +tests/cases/conformance/types/union/unionTypeMembers.ts(52,3): error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1 | I2'. +tests/cases/conformance/types/union/unionTypeMembers.ts(53,3): error TS2339: Property 'methodOnlyInI1' does not exist on type 'I1 | I2'. +tests/cases/conformance/types/union/unionTypeMembers.ts(54,3): error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1 | I2'. + + +==== tests/cases/conformance/types/union/unionTypeMembers.ts (5 errors) ==== + interface I1 { + commonMethodType(a: string): string; + commonPropertyType: string; + + commonMethodDifferentParameterType(a: string): string; + commonMethodDifferentReturnType(a: string): string; + commonPropertyDifferenType: string; + + commonMethodWithTypeParameter(a: T): T; + commonMethodWithOwnTypeParameter(a: U): U; + + methodOnlyInI1(a: string): string; + propertyOnlyInI1: string; + } + + interface I2 { + commonMethodType(a: string): string; + commonPropertyType: string; + + commonMethodDifferentParameterType(a: number): number; + commonMethodDifferentReturnType(a: string): number; + commonPropertyDifferenType: number; + + commonMethodWithTypeParameter(a: T): T; + commonMethodWithOwnTypeParameter(a: U): U; + + methodOnlyInI2(a: string): string; + propertyOnlyInI2: string; + } + + // a union type U has those members that are present in every one of its constituent types, + // with types that are unions of the respective members in the constituent types + var x : I1 | I2; + var str: string; + var num: number; + var strOrNum: string | number; + + // If each type in U has a property P, U has a property P of a union type of the types of P from each type in U. + str = x.commonPropertyType; // string + str = x.commonMethodType(str); // (a: string) => string so result should be string + strOrNum = x.commonPropertyDifferenType; + strOrNum = x.commonMethodDifferentReturnType(str); // string | union + x.commonMethodDifferentParameterType; // No error - property exists + x.commonMethodDifferentParameterType(strOrNum); // error - no call signatures because the type of this property is ((a: string) => string) | (a: number) => number + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. + // and the call signatures arent identical + num = x.commonMethodWithTypeParameter(num); + num = x.commonMethodWithOwnTypeParameter(num); + str = x.commonMethodWithOwnTypeParameter(str); + strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum); + + x.propertyOnlyInI1; // error + ~~~~~~~~~~~~~~~~ +!!! error TS2339: Property 'propertyOnlyInI1' does not exist on type 'I1 | I2'. + x.propertyOnlyInI2; // error + ~~~~~~~~~~~~~~~~ +!!! error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1 | I2'. + x.methodOnlyInI1("hello"); // error + ~~~~~~~~~~~~~~ +!!! error TS2339: Property 'methodOnlyInI1' does not exist on type 'I1 | I2'. + x.methodOnlyInI2(10); // error + ~~~~~~~~~~~~~~ +!!! error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1 | I2'. \ No newline at end of file diff --git a/tests/baselines/reference/unionTypeMembers.js b/tests/baselines/reference/unionTypeMembers.js new file mode 100644 index 00000000000..b03572246b4 --- /dev/null +++ b/tests/baselines/reference/unionTypeMembers.js @@ -0,0 +1,79 @@ +//// [unionTypeMembers.ts] +interface I1 { + commonMethodType(a: string): string; + commonPropertyType: string; + + commonMethodDifferentParameterType(a: string): string; + commonMethodDifferentReturnType(a: string): string; + commonPropertyDifferenType: string; + + commonMethodWithTypeParameter(a: T): T; + commonMethodWithOwnTypeParameter(a: U): U; + + methodOnlyInI1(a: string): string; + propertyOnlyInI1: string; +} + +interface I2 { + commonMethodType(a: string): string; + commonPropertyType: string; + + commonMethodDifferentParameterType(a: number): number; + commonMethodDifferentReturnType(a: string): number; + commonPropertyDifferenType: number; + + commonMethodWithTypeParameter(a: T): T; + commonMethodWithOwnTypeParameter(a: U): U; + + methodOnlyInI2(a: string): string; + propertyOnlyInI2: string; +} + +// a union type U has those members that are present in every one of its constituent types, +// with types that are unions of the respective members in the constituent types +var x : I1 | I2; +var str: string; +var num: number; +var strOrNum: string | number; + +// If each type in U has a property P, U has a property P of a union type of the types of P from each type in U. +str = x.commonPropertyType; // string +str = x.commonMethodType(str); // (a: string) => string so result should be string +strOrNum = x.commonPropertyDifferenType; +strOrNum = x.commonMethodDifferentReturnType(str); // string | union +x.commonMethodDifferentParameterType; // No error - property exists +x.commonMethodDifferentParameterType(strOrNum); // error - no call signatures because the type of this property is ((a: string) => string) | (a: number) => number + // and the call signatures arent identical +num = x.commonMethodWithTypeParameter(num); +num = x.commonMethodWithOwnTypeParameter(num); +str = x.commonMethodWithOwnTypeParameter(str); +strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum); + +x.propertyOnlyInI1; // error +x.propertyOnlyInI2; // error +x.methodOnlyInI1("hello"); // error +x.methodOnlyInI2(10); // error + +//// [unionTypeMembers.js] +// a union type U has those members that are present in every one of its constituent types, +// with types that are unions of the respective members in the constituent types +var x; +var str; +var num; +var strOrNum; +// If each type in U has a property P, U has a property P of a union type of the types of P from each type in U. +str = x.commonPropertyType; // string +str = x.commonMethodType(str); // (a: string) => string so result should be string +strOrNum = x.commonPropertyDifferenType; +strOrNum = x.commonMethodDifferentReturnType(str); // string | union +x.commonMethodDifferentParameterType; // No error - property exists +x.commonMethodDifferentParameterType(strOrNum); // error - no call signatures because the type of this property is ((a: string) => string) | (a: number) => number +// and the call signatures arent identical +num = x.commonMethodWithTypeParameter(num); +num = x.commonMethodWithOwnTypeParameter(num); +str = x.commonMethodWithOwnTypeParameter(str); +strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum); +x.propertyOnlyInI1; // error +x.propertyOnlyInI2; // error +x.methodOnlyInI1("hello"); // error +x.methodOnlyInI2(10); // error diff --git a/tests/cases/conformance/types/union/unionTypeEquivalence.ts b/tests/cases/conformance/types/union/unionTypeEquivalence.ts index 0905fde17f8..71fd6d91b65 100644 --- a/tests/cases/conformance/types/union/unionTypeEquivalence.ts +++ b/tests/cases/conformance/types/union/unionTypeEquivalence.ts @@ -1,6 +1,6 @@ // A | B is equivalent to A if B is a subtype of A class C { } -class D extends C { } +class D extends C { foo() { } } var x: C; var x : C | D; diff --git a/tests/cases/conformance/types/union/unionTypeMembers.ts b/tests/cases/conformance/types/union/unionTypeMembers.ts new file mode 100644 index 00000000000..73c6867e9ac --- /dev/null +++ b/tests/cases/conformance/types/union/unionTypeMembers.ts @@ -0,0 +1,54 @@ +interface I1 { + commonMethodType(a: string): string; + commonPropertyType: string; + + commonMethodDifferentParameterType(a: string): string; + commonMethodDifferentReturnType(a: string): string; + commonPropertyDifferenType: string; + + commonMethodWithTypeParameter(a: T): T; + commonMethodWithOwnTypeParameter(a: U): U; + + methodOnlyInI1(a: string): string; + propertyOnlyInI1: string; +} + +interface I2 { + commonMethodType(a: string): string; + commonPropertyType: string; + + commonMethodDifferentParameterType(a: number): number; + commonMethodDifferentReturnType(a: string): number; + commonPropertyDifferenType: number; + + commonMethodWithTypeParameter(a: T): T; + commonMethodWithOwnTypeParameter(a: U): U; + + methodOnlyInI2(a: string): string; + propertyOnlyInI2: string; +} + +// a union type U has those members that are present in every one of its constituent types, +// with types that are unions of the respective members in the constituent types +var x : I1 | I2; +var str: string; +var num: number; +var strOrNum: string | number; + +// If each type in U has a property P, U has a property P of a union type of the types of P from each type in U. +str = x.commonPropertyType; // string +str = x.commonMethodType(str); // (a: string) => string so result should be string +strOrNum = x.commonPropertyDifferenType; +strOrNum = x.commonMethodDifferentReturnType(str); // string | union +x.commonMethodDifferentParameterType; // No error - property exists +x.commonMethodDifferentParameterType(strOrNum); // error - no call signatures because the type of this property is ((a: string) => string) | (a: number) => number + // and the call signatures arent identical +num = x.commonMethodWithTypeParameter(num); +num = x.commonMethodWithOwnTypeParameter(num); +str = x.commonMethodWithOwnTypeParameter(str); +strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum); + +x.propertyOnlyInI1; // error +x.propertyOnlyInI2; // error +x.methodOnlyInI1("hello"); // error +x.methodOnlyInI2(10); // error \ No newline at end of file