diff --git a/tests/baselines/reference/unionTypeConstructSignatures.errors.txt b/tests/baselines/reference/unionTypeConstructSignatures.errors.txt new file mode 100644 index 0000000000..dd46564d77 --- /dev/null +++ b/tests/baselines/reference/unionTypeConstructSignatures.errors.txt @@ -0,0 +1,178 @@ +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(9,47): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(10,33): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(15,33): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(16,1): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(19,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(20,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(21,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(24,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(25,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(26,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(29,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(30,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(31,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(36,53): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(37,12): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(40,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(41,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(42,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(43,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(46,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(47,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(48,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(49,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(55,49): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(56,12): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(59,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(60,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(61,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(62,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(63,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(66,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(67,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(68,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(69,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/conformance/types/union/unionTypeConstructSignatures.ts(70,12): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + +==== tests/cases/conformance/types/union/unionTypeConstructSignatures.ts (35 errors) ==== + var numOrDate: number | Date; + var strOrBoolean: string | boolean; + var strOrNum: string | number; + + // If each type in U has construct signatures and the sets of construct signatures are identical ignoring return types, + // U has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in U. + var unionOfDifferentReturnType: { new (a: number): number; } | { new (a: number): Date; }; + numOrDate = new unionOfDifferentReturnType(10); + strOrBoolean = new unionOfDifferentReturnType("hello"); // error + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. + new unionOfDifferentReturnType1(true); // error in type of parameter + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + + var unionOfDifferentReturnType1: { new (a: number): number; new (a: string): string; } | { new (a: number): Date; new (a: string): boolean; }; + numOrDate = new unionOfDifferentReturnType1(10); + strOrBoolean = new unionOfDifferentReturnType1("hello"); + new unionOfDifferentReturnType1(true); // error in type of parameter + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + new unionOfDifferentReturnType1(); // error missing parameter + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + + var unionOfDifferentParameterTypes: { new (a: number): number; } | { new (a: string): Date; }; + new unionOfDifferentParameterTypes(10);// error - no call signatures + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + new unionOfDifferentParameterTypes("hello");// error - no call signatures + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + new unionOfDifferentParameterTypes();// error - no call signatures + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + var unionOfDifferentNumberOfSignatures: { new (a: number): number; } | { new (a: number): Date; new (a: string): boolean; }; + new unionOfDifferentNumberOfSignatures(); // error - no call signatures + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + new unionOfDifferentNumberOfSignatures(10); // error - no call signatures + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + new unionOfDifferentNumberOfSignatures("hello"); // error - no call signatures + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + var unionWithDifferentParameterCount: { new (a: string): string; } | { new (a: string, b: number): number; }; + new unionWithDifferentParameterCount();// no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + new unionWithDifferentParameterCount("hello");// no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + new unionWithDifferentParameterCount("hello", 10);// no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + var unionWithOptionalParameter1: { new (a: string, b?: number): string; } | { new (a: string, b?: number): number; }; + strOrNum = new unionWithOptionalParameter1('hello'); + strOrNum = new unionWithOptionalParameter1('hello', 10); + strOrNum = new unionWithOptionalParameter1('hello', "hello"); // error in parameter type + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. + strOrNum = new unionWithOptionalParameter1(); // error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + + var unionWithOptionalParameter2: { new (a: string, b?: number): string; } | { new (a: string, b: number): number }; + strOrNum = new unionWithOptionalParameter2('hello'); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithOptionalParameter2('hello', 10); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithOptionalParameter2('hello', "hello"); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithOptionalParameter2(); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + var unionWithOptionalParameter3: { new (a: string, b?: number): string; } | { new (a: string): number; }; + strOrNum = new unionWithOptionalParameter3('hello'); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithOptionalParameter3('hello', 10); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithOptionalParameter3('hello', "hello"); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithOptionalParameter3(); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + var unionWithRestParameter1: { new (a: string, ...b: number[]): string; } | { new (a: string, ...b: number[]): number }; + strOrNum = new unionWithRestParameter1('hello'); + strOrNum = new unionWithRestParameter1('hello', 10); + strOrNum = new unionWithRestParameter1('hello', 10, 11); + strOrNum = new unionWithRestParameter1('hello', "hello"); // error in parameter type + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. + strOrNum = new unionWithRestParameter1(); // error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + + var unionWithRestParameter2: { new (a: string, ...b: number[]): string; } | { new (a: string, b: number): number }; + strOrNum = new unionWithRestParameter2('hello'); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter2('hello', 10); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter2('hello', 10, 11); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter2('hello', "hello"); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter2(); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + var unionWithRestParameter3: { new (a: string, ...b: number[]): string; } | { new (a: string): number }; + strOrNum = new unionWithRestParameter3('hello'); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter3('hello', 10); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter3('hello', 10, 11); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter3('hello', "hello"); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + strOrNum = new unionWithRestParameter3(); // error no call signature + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. \ No newline at end of file diff --git a/tests/baselines/reference/unionTypeConstructSignatures.js b/tests/baselines/reference/unionTypeConstructSignatures.js new file mode 100644 index 0000000000..83c3df4460 --- /dev/null +++ b/tests/baselines/reference/unionTypeConstructSignatures.js @@ -0,0 +1,132 @@ +//// [unionTypeConstructSignatures.ts] +var numOrDate: number | Date; +var strOrBoolean: string | boolean; +var strOrNum: string | number; + +// If each type in U has construct signatures and the sets of construct signatures are identical ignoring return types, +// U has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in U. +var unionOfDifferentReturnType: { new (a: number): number; } | { new (a: number): Date; }; +numOrDate = new unionOfDifferentReturnType(10); +strOrBoolean = new unionOfDifferentReturnType("hello"); // error +new unionOfDifferentReturnType1(true); // error in type of parameter + +var unionOfDifferentReturnType1: { new (a: number): number; new (a: string): string; } | { new (a: number): Date; new (a: string): boolean; }; +numOrDate = new unionOfDifferentReturnType1(10); +strOrBoolean = new unionOfDifferentReturnType1("hello"); +new unionOfDifferentReturnType1(true); // error in type of parameter +new unionOfDifferentReturnType1(); // error missing parameter + +var unionOfDifferentParameterTypes: { new (a: number): number; } | { new (a: string): Date; }; +new unionOfDifferentParameterTypes(10);// error - no call signatures +new unionOfDifferentParameterTypes("hello");// error - no call signatures +new unionOfDifferentParameterTypes();// error - no call signatures + +var unionOfDifferentNumberOfSignatures: { new (a: number): number; } | { new (a: number): Date; new (a: string): boolean; }; +new unionOfDifferentNumberOfSignatures(); // error - no call signatures +new unionOfDifferentNumberOfSignatures(10); // error - no call signatures +new unionOfDifferentNumberOfSignatures("hello"); // error - no call signatures + +var unionWithDifferentParameterCount: { new (a: string): string; } | { new (a: string, b: number): number; }; +new unionWithDifferentParameterCount();// no call signature +new unionWithDifferentParameterCount("hello");// no call signature +new unionWithDifferentParameterCount("hello", 10);// no call signature + +var unionWithOptionalParameter1: { new (a: string, b?: number): string; } | { new (a: string, b?: number): number; }; +strOrNum = new unionWithOptionalParameter1('hello'); +strOrNum = new unionWithOptionalParameter1('hello', 10); +strOrNum = new unionWithOptionalParameter1('hello', "hello"); // error in parameter type +strOrNum = new unionWithOptionalParameter1(); // error + +var unionWithOptionalParameter2: { new (a: string, b?: number): string; } | { new (a: string, b: number): number }; +strOrNum = new unionWithOptionalParameter2('hello'); // error no call signature +strOrNum = new unionWithOptionalParameter2('hello', 10); // error no call signature +strOrNum = new unionWithOptionalParameter2('hello', "hello"); // error no call signature +strOrNum = new unionWithOptionalParameter2(); // error no call signature + +var unionWithOptionalParameter3: { new (a: string, b?: number): string; } | { new (a: string): number; }; +strOrNum = new unionWithOptionalParameter3('hello'); // error no call signature +strOrNum = new unionWithOptionalParameter3('hello', 10); // error no call signature +strOrNum = new unionWithOptionalParameter3('hello', "hello"); // error no call signature +strOrNum = new unionWithOptionalParameter3(); // error no call signature + +var unionWithRestParameter1: { new (a: string, ...b: number[]): string; } | { new (a: string, ...b: number[]): number }; +strOrNum = new unionWithRestParameter1('hello'); +strOrNum = new unionWithRestParameter1('hello', 10); +strOrNum = new unionWithRestParameter1('hello', 10, 11); +strOrNum = new unionWithRestParameter1('hello', "hello"); // error in parameter type +strOrNum = new unionWithRestParameter1(); // error + +var unionWithRestParameter2: { new (a: string, ...b: number[]): string; } | { new (a: string, b: number): number }; +strOrNum = new unionWithRestParameter2('hello'); // error no call signature +strOrNum = new unionWithRestParameter2('hello', 10); // error no call signature +strOrNum = new unionWithRestParameter2('hello', 10, 11); // error no call signature +strOrNum = new unionWithRestParameter2('hello', "hello"); // error no call signature +strOrNum = new unionWithRestParameter2(); // error no call signature + +var unionWithRestParameter3: { new (a: string, ...b: number[]): string; } | { new (a: string): number }; +strOrNum = new unionWithRestParameter3('hello'); // error no call signature +strOrNum = new unionWithRestParameter3('hello', 10); // error no call signature +strOrNum = new unionWithRestParameter3('hello', 10, 11); // error no call signature +strOrNum = new unionWithRestParameter3('hello', "hello"); // error no call signature +strOrNum = new unionWithRestParameter3(); // error no call signature + +//// [unionTypeConstructSignatures.js] +var numOrDate; +var strOrBoolean; +var strOrNum; +// If each type in U has construct signatures and the sets of construct signatures are identical ignoring return types, +// U has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in U. +var unionOfDifferentReturnType; +numOrDate = new unionOfDifferentReturnType(10); +strOrBoolean = new unionOfDifferentReturnType("hello"); // error +new unionOfDifferentReturnType1(true); // error in type of parameter +var unionOfDifferentReturnType1; +numOrDate = new unionOfDifferentReturnType1(10); +strOrBoolean = new unionOfDifferentReturnType1("hello"); +new unionOfDifferentReturnType1(true); // error in type of parameter +new unionOfDifferentReturnType1(); // error missing parameter +var unionOfDifferentParameterTypes; +new unionOfDifferentParameterTypes(10); // error - no call signatures +new unionOfDifferentParameterTypes("hello"); // error - no call signatures +new unionOfDifferentParameterTypes(); // error - no call signatures +var unionOfDifferentNumberOfSignatures; +new unionOfDifferentNumberOfSignatures(); // error - no call signatures +new unionOfDifferentNumberOfSignatures(10); // error - no call signatures +new unionOfDifferentNumberOfSignatures("hello"); // error - no call signatures +var unionWithDifferentParameterCount; +new unionWithDifferentParameterCount(); // no call signature +new unionWithDifferentParameterCount("hello"); // no call signature +new unionWithDifferentParameterCount("hello", 10); // no call signature +var unionWithOptionalParameter1; +strOrNum = new unionWithOptionalParameter1('hello'); +strOrNum = new unionWithOptionalParameter1('hello', 10); +strOrNum = new unionWithOptionalParameter1('hello', "hello"); // error in parameter type +strOrNum = new unionWithOptionalParameter1(); // error +var unionWithOptionalParameter2; +strOrNum = new unionWithOptionalParameter2('hello'); // error no call signature +strOrNum = new unionWithOptionalParameter2('hello', 10); // error no call signature +strOrNum = new unionWithOptionalParameter2('hello', "hello"); // error no call signature +strOrNum = new unionWithOptionalParameter2(); // error no call signature +var unionWithOptionalParameter3; +strOrNum = new unionWithOptionalParameter3('hello'); // error no call signature +strOrNum = new unionWithOptionalParameter3('hello', 10); // error no call signature +strOrNum = new unionWithOptionalParameter3('hello', "hello"); // error no call signature +strOrNum = new unionWithOptionalParameter3(); // error no call signature +var unionWithRestParameter1; +strOrNum = new unionWithRestParameter1('hello'); +strOrNum = new unionWithRestParameter1('hello', 10); +strOrNum = new unionWithRestParameter1('hello', 10, 11); +strOrNum = new unionWithRestParameter1('hello', "hello"); // error in parameter type +strOrNum = new unionWithRestParameter1(); // error +var unionWithRestParameter2; +strOrNum = new unionWithRestParameter2('hello'); // error no call signature +strOrNum = new unionWithRestParameter2('hello', 10); // error no call signature +strOrNum = new unionWithRestParameter2('hello', 10, 11); // error no call signature +strOrNum = new unionWithRestParameter2('hello', "hello"); // error no call signature +strOrNum = new unionWithRestParameter2(); // error no call signature +var unionWithRestParameter3; +strOrNum = new unionWithRestParameter3('hello'); // error no call signature +strOrNum = new unionWithRestParameter3('hello', 10); // error no call signature +strOrNum = new unionWithRestParameter3('hello', 10, 11); // error no call signature +strOrNum = new unionWithRestParameter3('hello', "hello"); // error no call signature +strOrNum = new unionWithRestParameter3(); // error no call signature diff --git a/tests/cases/conformance/types/union/unionTypeConstructSignatures.ts b/tests/cases/conformance/types/union/unionTypeConstructSignatures.ts new file mode 100644 index 0000000000..a02ba610ff --- /dev/null +++ b/tests/cases/conformance/types/union/unionTypeConstructSignatures.ts @@ -0,0 +1,70 @@ +var numOrDate: number | Date; +var strOrBoolean: string | boolean; +var strOrNum: string | number; + +// If each type in U has construct signatures and the sets of construct signatures are identical ignoring return types, +// U has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in U. +var unionOfDifferentReturnType: { new (a: number): number; } | { new (a: number): Date; }; +numOrDate = new unionOfDifferentReturnType(10); +strOrBoolean = new unionOfDifferentReturnType("hello"); // error +new unionOfDifferentReturnType1(true); // error in type of parameter + +var unionOfDifferentReturnType1: { new (a: number): number; new (a: string): string; } | { new (a: number): Date; new (a: string): boolean; }; +numOrDate = new unionOfDifferentReturnType1(10); +strOrBoolean = new unionOfDifferentReturnType1("hello"); +new unionOfDifferentReturnType1(true); // error in type of parameter +new unionOfDifferentReturnType1(); // error missing parameter + +var unionOfDifferentParameterTypes: { new (a: number): number; } | { new (a: string): Date; }; +new unionOfDifferentParameterTypes(10);// error - no call signatures +new unionOfDifferentParameterTypes("hello");// error - no call signatures +new unionOfDifferentParameterTypes();// error - no call signatures + +var unionOfDifferentNumberOfSignatures: { new (a: number): number; } | { new (a: number): Date; new (a: string): boolean; }; +new unionOfDifferentNumberOfSignatures(); // error - no call signatures +new unionOfDifferentNumberOfSignatures(10); // error - no call signatures +new unionOfDifferentNumberOfSignatures("hello"); // error - no call signatures + +var unionWithDifferentParameterCount: { new (a: string): string; } | { new (a: string, b: number): number; }; +new unionWithDifferentParameterCount();// no call signature +new unionWithDifferentParameterCount("hello");// no call signature +new unionWithDifferentParameterCount("hello", 10);// no call signature + +var unionWithOptionalParameter1: { new (a: string, b?: number): string; } | { new (a: string, b?: number): number; }; +strOrNum = new unionWithOptionalParameter1('hello'); +strOrNum = new unionWithOptionalParameter1('hello', 10); +strOrNum = new unionWithOptionalParameter1('hello', "hello"); // error in parameter type +strOrNum = new unionWithOptionalParameter1(); // error + +var unionWithOptionalParameter2: { new (a: string, b?: number): string; } | { new (a: string, b: number): number }; +strOrNum = new unionWithOptionalParameter2('hello'); // error no call signature +strOrNum = new unionWithOptionalParameter2('hello', 10); // error no call signature +strOrNum = new unionWithOptionalParameter2('hello', "hello"); // error no call signature +strOrNum = new unionWithOptionalParameter2(); // error no call signature + +var unionWithOptionalParameter3: { new (a: string, b?: number): string; } | { new (a: string): number; }; +strOrNum = new unionWithOptionalParameter3('hello'); // error no call signature +strOrNum = new unionWithOptionalParameter3('hello', 10); // error no call signature +strOrNum = new unionWithOptionalParameter3('hello', "hello"); // error no call signature +strOrNum = new unionWithOptionalParameter3(); // error no call signature + +var unionWithRestParameter1: { new (a: string, ...b: number[]): string; } | { new (a: string, ...b: number[]): number }; +strOrNum = new unionWithRestParameter1('hello'); +strOrNum = new unionWithRestParameter1('hello', 10); +strOrNum = new unionWithRestParameter1('hello', 10, 11); +strOrNum = new unionWithRestParameter1('hello', "hello"); // error in parameter type +strOrNum = new unionWithRestParameter1(); // error + +var unionWithRestParameter2: { new (a: string, ...b: number[]): string; } | { new (a: string, b: number): number }; +strOrNum = new unionWithRestParameter2('hello'); // error no call signature +strOrNum = new unionWithRestParameter2('hello', 10); // error no call signature +strOrNum = new unionWithRestParameter2('hello', 10, 11); // error no call signature +strOrNum = new unionWithRestParameter2('hello', "hello"); // error no call signature +strOrNum = new unionWithRestParameter2(); // error no call signature + +var unionWithRestParameter3: { new (a: string, ...b: number[]): string; } | { new (a: string): number }; +strOrNum = new unionWithRestParameter3('hello'); // error no call signature +strOrNum = new unionWithRestParameter3('hello', 10); // error no call signature +strOrNum = new unionWithRestParameter3('hello', 10, 11); // error no call signature +strOrNum = new unionWithRestParameter3('hello', "hello"); // error no call signature +strOrNum = new unionWithRestParameter3(); // error no call signature \ No newline at end of file