From bcddb101aa2a8c683c984e9a73c5c5444b063c35 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 5 Nov 2014 18:20:40 -0800 Subject: [PATCH] Proper type arg inference with apppropriate overload res tests. --- src/compiler/checker.ts | 24 ++- ...ateStringsTypeArgumentInference.errors.txt | 190 ++++++++++++++++++ ...StringsTypeArgumentInferenceES6.errors.txt | 105 ++++++++++ ...TemplateStringsTypeArgumentInferenceES6.js | 171 ++++++++++++++++ ...eStringsWithOverloadResolution2.errors.txt | 27 +++ ...plateStringsWithOverloadResolution2_ES6.js | 38 ++++ ...teStringsWithOverloadResolution2_ES6.types | 59 ++++++ ...eStringsWithOverloadResolution3.errors.txt | 160 +++++++++++++++ ...ingsWithOverloadResolution3_ES6.errors.txt | 93 +++++++++ ...plateStringsWithOverloadResolution3_ES6.js | 122 +++++++++++ ...gedTemplateStringsTypeArgumentInference.ts | 92 +++++++++ ...TemplateStringsTypeArgumentInferenceES6.ts | 92 +++++++++ ...dTemplateStringsWithOverloadResolution2.ts | 18 ++ ...plateStringsWithOverloadResolution2_ES6.ts | 18 ++ ...dTemplateStringsWithOverloadResolution3.ts | 72 +++++++ ...plateStringsWithOverloadResolution3_ES6.ts | 72 +++++++ 16 files changed, 1344 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/taggedTemplateStringsTypeArgumentInference.errors.txt create mode 100644 tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.errors.txt create mode 100644 tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js create mode 100644 tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2.errors.txt create mode 100644 tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.js create mode 100644 tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.types create mode 100644 tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3.errors.txt create mode 100644 tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.errors.txt create mode 100644 tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js create mode 100644 tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts create mode 100644 tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInferenceES6.ts create mode 100644 tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2.ts create mode 100644 tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2_ES6.ts create mode 100644 tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts create mode 100644 tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bd373006299..805a96a6b84 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5271,21 +5271,28 @@ module ts { var mapper = createInferenceMapper(context); // First infer from arguments that are not context sensitive for (var i = 0; i < args.length; i++) { - // TODO (drosen): This breaks hard when inferring on a tagged template. if (args[i].kind === SyntaxKind.OmittedExpression) { continue; } if (!excludeArgument || excludeArgument[i] === undefined) { var parameterType = getTypeAtPosition(signature, i); + + if (i === 0 && args[i].parent.kind === SyntaxKind.TaggedTemplateExpression) { + inferTypes(context, globalTemplateStringsArrayType, parameterType); + continue; + } + inferTypes(context, checkExpressionWithContextualType(args[i], parameterType, mapper), parameterType); } } + // Next, infer from those context sensitive arguments that are no longer excluded if (excludeArgument) { for (var i = 0; i < args.length; i++) { if (args[i].kind === SyntaxKind.OmittedExpression) { continue; } + // No need to special-case tagged templates; their excludeArgument value will be 'undefined'. if (excludeArgument[i] === false) { var parameterType = getTypeAtPosition(signature, i); inferTypes(context, checkExpressionWithContextualType(args[i], parameterType, mapper), parameterType); @@ -5325,19 +5332,20 @@ module ts { return typeArgumentsAreAssignable; } - function checkApplicableSignature(node: CallExpression | TaggedTemplateExpression, callArguments: Node[], signature: Signature, relation: Map, excludeArgument: boolean[], reportErrors: boolean) { - for (var i = 0; i < callArguments.length; i++) { - var arg = callArguments[i]; + function checkApplicableSignature(node: CallExpression | TaggedTemplateExpression, args: Node[], signature: Signature, relation: Map, excludeArgument: boolean[], reportErrors: boolean) { + for (var i = 0; i < args.length; i++) { + var arg = args[i]; var argType: Type; - if (arg && arg.kind === SyntaxKind.OmittedExpression) { + if (arg.kind === SyntaxKind.OmittedExpression) { continue; } var paramType = getTypeAtPosition(signature, i); if (i === 0 && node.kind === SyntaxKind.TaggedTemplateExpression) { - arg = (node).template; // just to report an error on the template + // A tagged template expression has something of a + // "virtual" parameter with the "cooked" strings array type. argType = globalTemplateStringsArrayType; } else { @@ -5369,9 +5377,7 @@ module ts { var args: Expression[]; if (node.kind === SyntaxKind.TaggedTemplateExpression) { var template = (node).template; - // REVIEW: Should this be undefined or template? - // I currently use 'undefined' mostly to catch places we are not accounting for. - args = [undefined]; + args = [template]; if (template.kind === SyntaxKind.TemplateExpression) { args.push.apply(args, map((template).templateSpans, span => span.expression)); diff --git a/tests/baselines/reference/taggedTemplateStringsTypeArgumentInference.errors.txt b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInference.errors.txt new file mode 100644 index 00000000000..c4a34fccd95 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInference.errors.txt @@ -0,0 +1,190 @@ +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(5,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(9,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(13,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(16,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(20,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(23,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(27,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(28,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(29,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(33,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(34,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(35,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(39,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(40,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(41,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(45,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(46,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(47,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(51,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(52,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(53,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(57,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(58,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(64,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(77,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(81,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(86,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(90,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(64,11): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. + Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(77,11): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. + Type argument candidate '{ x: number; z: Date; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y: string; }'. + + +==== tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts (30 errors) ==== + + + // Generic tag with one parameter + function noParams(n: T) { } + noParams ``; + ~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with parameter which does not use type parameter + function noGenericParams(n: string[]) { } + noGenericParams ``; + ~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with multiple type parameters and only one used in parameter type annotation + function someGenerics1a(n: T, m: number) { } + someGenerics1a `${3}`; + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + function someGenerics1b(n: string[], m: U) { } + someGenerics1b `${3}`; + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with argument of function type whose parameter is of type parameter type + function someGenerics2a(strs: string[], n: (x: T) => void) { } + someGenerics2a `${(n: string) => n}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + function someGenerics2b(strs: string[], n: (x: T, y: U) => void) { } + someGenerics2b `${ (n: string, x: number) => n }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter + function someGenerics3(strs: string[], producer: () => T) { } + someGenerics3 `${() => ''}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics3 `${() => undefined}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics3 `${() => 3}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type + function someGenerics4(strs: string[], n: T, f: (x: U) => void) { } + someGenerics4 `${4}${ () => null }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics4 `${''}${ () => 3 }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics4 `${ null }${ null }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type + function someGenerics5(strs: string[], n: T, f: (x: U) => void) { } + someGenerics5 `${ 4 } ${ () => null }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics5 `${ '' }${ () => 3 }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics5 `${null}${null}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with multiple arguments of function types that each have parameters of the same generic type + function someGenerics6(strs: string[], a: (a: A) => A, b: (b: A) => A, c: (c: A) => A) { } + someGenerics6 `${ n => n }${ n => n}${ n => n}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics6 `${ n => n }${ n => n}${ n => n}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics6 `${ (n: number) => n }${ (n: number) => n }${ (n: number) => n }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with multiple arguments of function types that each have parameters of different generic type + function someGenerics7(strs: string[], a: (a: A) => A, b: (b: B) => B, c: (c: C) => C) { } + someGenerics7 `${ n => n }${ n => n }${ n => n }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics7 `${ n => n }${ n => n }${ n => n }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + someGenerics7 `${(n: number) => n}${ (n: string) => n}${ (n: number) => n}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with argument of generic function type + function someGenerics8(strs: string[], n: T): T { return n; } + var x = someGenerics8 `${ someGenerics7 }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + x `${null}${null}${null}`; + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic tag with multiple parameters of generic type passed arguments with no best common type + function someGenerics9(strs: string[], a: T, b: T, c: T): T { + return null; + } + var a9a = someGenerics9 `${ '' }${ 0 }${ [] }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~~~~~~~~~~~~ +!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. +!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. + var a9a: {}; + + // Generic tag with multiple parameters of generic type passed arguments with multiple best common types + interface A91 { + x: number; + y?: string; + } + interface A92 { + x: number; + z?: Date; + } + + var a9e = someGenerics9 `${ undefined }${ { x: 6, z: new Date() } }${ { x: 6, y: '' } }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~~~~~~~~~~~~ +!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. +!!! error TS2453: Type argument candidate '{ x: number; z: Date; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y: string; }'. + var a9e: {}; + + // Generic tag with multiple parameters of generic type passed arguments with a single best common type + var a9d = someGenerics9 `${ { x: 3 }}${ { x: 6 }}${ { x: 6 } }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var a9d: { x: number; }; + + // Generic tag with multiple parameters of generic type where one argument is of type 'any' + var anyVar: any; + var a = someGenerics9 `${ 7 }${ anyVar }${ 4 }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var a: any; + + // Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any' + var arr = someGenerics9 `${ [] }${ null }${ undefined }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var arr: any[]; + + \ No newline at end of file diff --git a/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.errors.txt b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.errors.txt new file mode 100644 index 00000000000..170c10c7381 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.errors.txt @@ -0,0 +1,105 @@ +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInferenceES6.ts(63,11): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. + Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. +tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInferenceES6.ts(76,11): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. + Type argument candidate '{ x: number; z: Date; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y: string; }'. + + +==== tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInferenceES6.ts (2 errors) ==== + + // Generic tag with one parameter + function noParams(n: T) { } + noParams ``; + + // Generic tag with parameter which does not use type parameter + function noGenericParams(n: string[]) { } + noGenericParams ``; + + // Generic tag with multiple type parameters and only one used in parameter type annotation + function someGenerics1a(n: T, m: number) { } + someGenerics1a `${3}`; + + function someGenerics1b(n: string[], m: U) { } + someGenerics1b `${3}`; + + // Generic tag with argument of function type whose parameter is of type parameter type + function someGenerics2a(strs: string[], n: (x: T) => void) { } + someGenerics2a `${(n: string) => n}`; + + function someGenerics2b(strs: string[], n: (x: T, y: U) => void) { } + someGenerics2b `${ (n: string, x: number) => n }`; + + // Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter + function someGenerics3(strs: string[], producer: () => T) { } + someGenerics3 `${() => ''}`; + someGenerics3 `${() => undefined}`; + someGenerics3 `${() => 3}`; + + // 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type + function someGenerics4(strs: string[], n: T, f: (x: U) => void) { } + someGenerics4 `${4}${ () => null }`; + someGenerics4 `${''}${ () => 3 }`; + someGenerics4 `${ null }${ null }`; + + // 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type + function someGenerics5(strs: string[], n: T, f: (x: U) => void) { } + someGenerics5 `${ 4 } ${ () => null }`; + someGenerics5 `${ '' }${ () => 3 }`; + someGenerics5 `${null}${null}`; + + // Generic tag with multiple arguments of function types that each have parameters of the same generic type + function someGenerics6(strs: string[], a: (a: A) => A, b: (b: A) => A, c: (c: A) => A) { } + someGenerics6 `${ n => n }${ n => n}${ n => n}`; + someGenerics6 `${ n => n }${ n => n}${ n => n}`; + someGenerics6 `${ (n: number) => n }${ (n: number) => n }${ (n: number) => n }`; + + // Generic tag with multiple arguments of function types that each have parameters of different generic type + function someGenerics7(strs: string[], a: (a: A) => A, b: (b: B) => B, c: (c: C) => C) { } + someGenerics7 `${ n => n }${ n => n }${ n => n }`; + someGenerics7 `${ n => n }${ n => n }${ n => n }`; + someGenerics7 `${(n: number) => n}${ (n: string) => n}${ (n: number) => n}`; + + // Generic tag with argument of generic function type + function someGenerics8(strs: string[], n: T): T { return n; } + var x = someGenerics8 `${ someGenerics7 }`; + x `${null}${null}${null}`; + + // Generic tag with multiple parameters of generic type passed arguments with no best common type + function someGenerics9(strs: string[], a: T, b: T, c: T): T { + return null; + } + var a9a = someGenerics9 `${ '' }${ 0 }${ [] }`; + ~~~~~~~~~~~~~ +!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. +!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. + var a9a: {}; + + // Generic tag with multiple parameters of generic type passed arguments with multiple best common types + interface A91 { + x: number; + y?: string; + } + interface A92 { + x: number; + z?: Date; + } + + var a9e = someGenerics9 `${ undefined }${ { x: 6, z: new Date() } }${ { x: 6, y: '' } }`; + ~~~~~~~~~~~~~ +!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. +!!! error TS2453: Type argument candidate '{ x: number; z: Date; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y: string; }'. + var a9e: {}; + + // Generic tag with multiple parameters of generic type passed arguments with a single best common type + var a9d = someGenerics9 `${ { x: 3 }}${ { x: 6 }}${ { x: 6 } }`; + var a9d: { x: number; }; + + // Generic tag with multiple parameters of generic type where one argument is of type 'any' + var anyVar: any; + var a = someGenerics9 `${ 7 }${ anyVar }${ 4 }`; + var a: any; + + // Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any' + var arr = someGenerics9 `${ [] }${ null }${ undefined }`; + var arr: any[]; + + \ No newline at end of file diff --git a/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js new file mode 100644 index 00000000000..b88eadfd80b --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js @@ -0,0 +1,171 @@ +//// [taggedTemplateStringsTypeArgumentInferenceES6.ts] + +// Generic tag with one parameter +function noParams(n: T) { } +noParams ``; + +// Generic tag with parameter which does not use type parameter +function noGenericParams(n: string[]) { } +noGenericParams ``; + +// Generic tag with multiple type parameters and only one used in parameter type annotation +function someGenerics1a(n: T, m: number) { } +someGenerics1a `${3}`; + +function someGenerics1b(n: string[], m: U) { } +someGenerics1b `${3}`; + +// Generic tag with argument of function type whose parameter is of type parameter type +function someGenerics2a(strs: string[], n: (x: T) => void) { } +someGenerics2a `${(n: string) => n}`; + +function someGenerics2b(strs: string[], n: (x: T, y: U) => void) { } +someGenerics2b `${ (n: string, x: number) => n }`; + +// Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter +function someGenerics3(strs: string[], producer: () => T) { } +someGenerics3 `${() => ''}`; +someGenerics3 `${() => undefined}`; +someGenerics3 `${() => 3}`; + +// 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type +function someGenerics4(strs: string[], n: T, f: (x: U) => void) { } +someGenerics4 `${4}${ () => null }`; +someGenerics4 `${''}${ () => 3 }`; +someGenerics4 `${ null }${ null }`; + +// 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type +function someGenerics5(strs: string[], n: T, f: (x: U) => void) { } +someGenerics5 `${ 4 } ${ () => null }`; +someGenerics5 `${ '' }${ () => 3 }`; +someGenerics5 `${null}${null}`; + +// Generic tag with multiple arguments of function types that each have parameters of the same generic type +function someGenerics6(strs: string[], a: (a: A) => A, b: (b: A) => A, c: (c: A) => A) { } +someGenerics6 `${ n => n }${ n => n}${ n => n}`; +someGenerics6 `${ n => n }${ n => n}${ n => n}`; +someGenerics6 `${ (n: number) => n }${ (n: number) => n }${ (n: number) => n }`; + +// Generic tag with multiple arguments of function types that each have parameters of different generic type +function someGenerics7(strs: string[], a: (a: A) => A, b: (b: B) => B, c: (c: C) => C) { } +someGenerics7 `${ n => n }${ n => n }${ n => n }`; +someGenerics7 `${ n => n }${ n => n }${ n => n }`; +someGenerics7 `${(n: number) => n}${ (n: string) => n}${ (n: number) => n}`; + +// Generic tag with argument of generic function type +function someGenerics8(strs: string[], n: T): T { return n; } +var x = someGenerics8 `${ someGenerics7 }`; +x `${null}${null}${null}`; + +// Generic tag with multiple parameters of generic type passed arguments with no best common type +function someGenerics9(strs: string[], a: T, b: T, c: T): T { + return null; +} +var a9a = someGenerics9 `${ '' }${ 0 }${ [] }`; +var a9a: {}; + +// Generic tag with multiple parameters of generic type passed arguments with multiple best common types +interface A91 { + x: number; + y?: string; +} +interface A92 { + x: number; + z?: Date; +} + +var a9e = someGenerics9 `${ undefined }${ { x: 6, z: new Date() } }${ { x: 6, y: '' } }`; +var a9e: {}; + +// Generic tag with multiple parameters of generic type passed arguments with a single best common type +var a9d = someGenerics9 `${ { x: 3 }}${ { x: 6 }}${ { x: 6 } }`; +var a9d: { x: number; }; + +// Generic tag with multiple parameters of generic type where one argument is of type 'any' +var anyVar: any; +var a = someGenerics9 `${ 7 }${ anyVar }${ 4 }`; +var a: any; + +// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any' +var arr = someGenerics9 `${ [] }${ null }${ undefined }`; +var arr: any[]; + + + +//// [taggedTemplateStringsTypeArgumentInferenceES6.js] +// Generic tag with one parameter +function noParams(n) { +} +noParams ``; +// Generic tag with parameter which does not use type parameter +function noGenericParams(n) { +} +noGenericParams ``; +// Generic tag with multiple type parameters and only one used in parameter type annotation +function someGenerics1a(n, m) { +} +someGenerics1a `${3}`; +function someGenerics1b(n, m) { +} +someGenerics1b `${3}`; +// Generic tag with argument of function type whose parameter is of type parameter type +function someGenerics2a(strs, n) { +} +someGenerics2a `${function (n) { return n; }}`; +function someGenerics2b(strs, n) { +} +someGenerics2b `${function (n, x) { return n; }}`; +// Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter +function someGenerics3(strs, producer) { +} +someGenerics3 `${function () { return ''; }}`; +someGenerics3 `${function () { return undefined; }}`; +someGenerics3 `${function () { return 3; }}`; +// 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type +function someGenerics4(strs, n, f) { +} +someGenerics4 `${4}${function () { return null; }}`; +someGenerics4 `${''}${function () { return 3; }}`; +someGenerics4 `${null}${null}`; +// 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type +function someGenerics5(strs, n, f) { +} +someGenerics5 `${4} ${function () { return null; }}`; +someGenerics5 `${''}${function () { return 3; }}`; +someGenerics5 `${null}${null}`; +// Generic tag with multiple arguments of function types that each have parameters of the same generic type +function someGenerics6(strs, a, b, c) { +} +someGenerics6 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`; +someGenerics6 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`; +someGenerics6 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`; +// Generic tag with multiple arguments of function types that each have parameters of different generic type +function someGenerics7(strs, a, b, c) { +} +someGenerics7 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`; +someGenerics7 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`; +someGenerics7 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`; +// Generic tag with argument of generic function type +function someGenerics8(strs, n) { + return n; +} +var x = someGenerics8 `${someGenerics7}`; +x `${null}${null}${null}`; +// Generic tag with multiple parameters of generic type passed arguments with no best common type +function someGenerics9(strs, a, b, c) { + return null; +} +var a9a = someGenerics9 `${''}${0}${[]}`; +var a9a; +var a9e = someGenerics9 `${undefined}${{ x: 6, z: new Date() }}${{ x: 6, y: '' }}`; +var a9e; +// Generic tag with multiple parameters of generic type passed arguments with a single best common type +var a9d = someGenerics9 `${{ x: 3 }}${{ x: 6 }}${{ x: 6 }}`; +var a9d; +// Generic tag with multiple parameters of generic type where one argument is of type 'any' +var anyVar; +var a = someGenerics9 `${7}${anyVar}${4}`; +var a; +// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any' +var arr = someGenerics9 `${[]}${null}${undefined}`; +var arr; diff --git a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2.errors.txt b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2.errors.txt new file mode 100644 index 00000000000..fcde15afc27 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2.ts(8,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2.ts(17,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + +==== tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2.ts (2 errors) ==== + + function foo1(strs: TemplateStringsArray, x: number): string; + function foo1(strs: string[], x: number): number; + function foo1(...stuff: any[]): any { + return undefined; + } + + var a = foo1 `${1}`; // string + ~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var b = foo1([], 1); // number + + function foo2(strs: string[], x: number): number; + function foo2(strs: TemplateStringsArray, x: number): string; + function foo2(...stuff: any[]): any { + return undefined; + } + + var c = foo2 `${1}`; // number + ~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var d = foo2([], 1); // number \ No newline at end of file diff --git a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.js b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.js new file mode 100644 index 00000000000..44da93f8041 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.js @@ -0,0 +1,38 @@ +//// [taggedTemplateStringsWithOverloadResolution2_ES6.ts] +function foo1(strs: TemplateStringsArray, x: number): string; +function foo1(strs: string[], x: number): number; +function foo1(...stuff: any[]): any { + return undefined; +} + +var a = foo1 `${1}`; // string +var b = foo1([], 1); // number + +function foo2(strs: string[], x: number): number; +function foo2(strs: TemplateStringsArray, x: number): string; +function foo2(...stuff: any[]): any { + return undefined; +} + +var c = foo2 `${1}`; // number +var d = foo2([], 1); // number + +//// [taggedTemplateStringsWithOverloadResolution2_ES6.js] +function foo1() { + var stuff = []; + for (var _i = 0; _i < arguments.length; _i++) { + stuff[_i - 0] = arguments[_i]; + } + return undefined; +} +var a = foo1 `${1}`; // string +var b = foo1([], 1); // number +function foo2() { + var stuff = []; + for (var _i = 0; _i < arguments.length; _i++) { + stuff[_i - 0] = arguments[_i]; + } + return undefined; +} +var c = foo2 `${1}`; // number +var d = foo2([], 1); // number diff --git a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.types b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.types new file mode 100644 index 00000000000..d9d918b37bd --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution2_ES6.types @@ -0,0 +1,59 @@ +=== tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2_ES6.ts === +function foo1(strs: TemplateStringsArray, x: number): string; +>foo1 : { (strs: TemplateStringsArray, x: number): string; (strs: string[], x: number): number; } +>strs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>x : number + +function foo1(strs: string[], x: number): number; +>foo1 : { (strs: TemplateStringsArray, x: number): string; (strs: string[], x: number): number; } +>strs : string[] +>x : number + +function foo1(...stuff: any[]): any { +>foo1 : { (strs: TemplateStringsArray, x: number): string; (strs: string[], x: number): number; } +>stuff : any[] + + return undefined; +>undefined : undefined +} + +var a = foo1 `${1}`; // string +>a : string +>foo1 : { (strs: TemplateStringsArray, x: number): string; (strs: string[], x: number): number; } + +var b = foo1([], 1); // number +>b : number +>foo1([], 1) : number +>foo1 : { (strs: TemplateStringsArray, x: number): string; (strs: string[], x: number): number; } +>[] : undefined[] + +function foo2(strs: string[], x: number): number; +>foo2 : { (strs: string[], x: number): number; (strs: TemplateStringsArray, x: number): string; } +>strs : string[] +>x : number + +function foo2(strs: TemplateStringsArray, x: number): string; +>foo2 : { (strs: string[], x: number): number; (strs: TemplateStringsArray, x: number): string; } +>strs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>x : number + +function foo2(...stuff: any[]): any { +>foo2 : { (strs: string[], x: number): number; (strs: TemplateStringsArray, x: number): string; } +>stuff : any[] + + return undefined; +>undefined : undefined +} + +var c = foo2 `${1}`; // number +>c : number +>foo2 : { (strs: string[], x: number): number; (strs: TemplateStringsArray, x: number): string; } + +var d = foo2([], 1); // number +>d : number +>foo2([], 1) : number +>foo2 : { (strs: string[], x: number): number; (strs: TemplateStringsArray, x: number): string; } +>[] : undefined[] + diff --git a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3.errors.txt b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3.errors.txt new file mode 100644 index 00000000000..dd943e518f3 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3.errors.txt @@ -0,0 +1,160 @@ +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(7,17): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(10,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(16,16): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(17,16): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(23,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(26,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(34,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(35,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(36,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(40,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(41,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(42,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(45,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(54,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(55,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(56,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(57,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(60,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(63,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(64,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(70,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(71,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(10,9): error TS2345: Argument of type '{}' is not assignable to parameter of type 'number'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(19,4): error TS2339: Property 'foo' does not exist on type 'Date'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(45,1): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(63,9): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(64,18): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts(70,18): error TS2339: Property 'toFixed' does not exist on type 'string'. + + +==== tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts (28 errors) ==== + + // Ambiguous call picks the first overload in declaration order + function fn1(strs: TemplateStringsArray, s: string): string; + function fn1(strs: TemplateStringsArray, n: number): number; + function fn1() { return null; } + + var s: string = fn1 `${ undefined }`; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // No candidate overloads found + fn1 `${ {} }`; // Error + ~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'number'. + + function fn2(strs: TemplateStringsArray, s: string, n: number): number; + function fn2(strs: TemplateStringsArray, n: number, t: T): T; + function fn2() { return undefined; } + + var d1: Date = fn2 `${ 0 }${ undefined }`; // contextually typed + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var d2 = fn2 `${ 0 }${ undefined }`; // any + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + d1.foo(); // error + ~~~ +!!! error TS2339: Property 'foo' does not exist on type 'Date'. + d2(); // no error (typed as any) + + // Generic and non-generic overload where generic overload is the only candidate + fn2 `${ 0 }${ '' }`; // OK + ~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic and non-generic overload where non-generic overload is the only candidate + fn2 `${ '' }${ 0 }`; // OK + ~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic overloads with differing arity + function fn3(strs: TemplateStringsArray, n: T): string; + function fn3(strs: TemplateStringsArray, s: string, t: T, u: U): U; + function fn3(strs: TemplateStringsArray, v: V, u: U, t: T): number; + function fn3() { return null; } + + var s = fn3 `${ 3 }`; + ~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var s = fn3 `${'' }${ 3 }${ '' }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var n = fn3 `${ 5 }${ 5 }${ 5 }`; + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var n: number; + + // Generic overloads with differing arity tagging with arguments matching each overload type parameter count + var s = fn3 `${ 4 }` + ~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var s = fn3 `${ '' }${ '' }${ '' }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + var n = fn3 `${ '' }${ '' }${ 3 }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic overloads with differing arity tagging with argument count that doesn't match any overload + fn3 ``; // Error + ~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + + // Generic overloads with constraints + function fn4(strs: TemplateStringsArray, n: T, m: U); + function fn4(strs: TemplateStringsArray, n: T, m: U); + function fn4(strs: TemplateStringsArray) + function fn4() { } + + // Generic overloads with constraints tagged with types that satisfy the constraints + fn4 `${ '' }${ 3 }`; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + fn4 `${ 3 }${ '' }`; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + fn4 `${ 3 }${ undefined }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + fn4 `${ '' }${ null }`; + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic overloads with constraints called with type arguments that do not satisfy the constraints + fn4 `${ null }${ null }`; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + // Generic overloads with constraints called without type arguments but with types that do not satisfy the constraints + fn4 `${ true }${ null }`; + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number'. + fn4 `${ null }${ true }`; + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + + // Non - generic overloads where contextual typing of function arguments has errors + function fn5(strs: TemplateStringsArray, f: (n: string) => void): string; + function fn5(strs: TemplateStringsArray, f: (n: number) => void): number; + function fn5() { return undefined; } + fn5 `${ (n) => n.toFixed() }`; // will error; 'n' should have type 'string'. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~~~~~~ +!!! error TS2339: Property 'toFixed' does not exist on type 'string'. + fn5 `${ (n) => n.substr(0) }`; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + + \ No newline at end of file diff --git a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.errors.txt b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.errors.txt new file mode 100644 index 00000000000..e65acdee835 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.errors.txt @@ -0,0 +1,93 @@ +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts(9,9): error TS2345: Argument of type '{}' is not assignable to parameter of type 'number'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts(18,4): error TS2339: Property 'foo' does not exist on type 'Date'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts(44,1): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts(62,9): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts(63,18): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts(69,18): error TS2339: Property 'toFixed' does not exist on type 'string'. + + +==== tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts (6 errors) ==== + // Ambiguous call picks the first overload in declaration order + function fn1(strs: TemplateStringsArray, s: string): string; + function fn1(strs: TemplateStringsArray, n: number): number; + function fn1() { return null; } + + var s: string = fn1 `${ undefined }`; + + // No candidate overloads found + fn1 `${ {} }`; // Error + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'number'. + + function fn2(strs: TemplateStringsArray, s: string, n: number): number; + function fn2(strs: TemplateStringsArray, n: number, t: T): T; + function fn2() { return undefined; } + + var d1: Date = fn2 `${ 0 }${ undefined }`; // contextually typed + var d2 = fn2 `${ 0 }${ undefined }`; // any + + d1.foo(); // error + ~~~ +!!! error TS2339: Property 'foo' does not exist on type 'Date'. + d2(); // no error (typed as any) + + // Generic and non-generic overload where generic overload is the only candidate + fn2 `${ 0 }${ '' }`; // OK + + // Generic and non-generic overload where non-generic overload is the only candidate + fn2 `${ '' }${ 0 }`; // OK + + // Generic overloads with differing arity + function fn3(strs: TemplateStringsArray, n: T): string; + function fn3(strs: TemplateStringsArray, s: string, t: T, u: U): U; + function fn3(strs: TemplateStringsArray, v: V, u: U, t: T): number; + function fn3() { return null; } + + var s = fn3 `${ 3 }`; + var s = fn3 `${'' }${ 3 }${ '' }`; + var n = fn3 `${ 5 }${ 5 }${ 5 }`; + var n: number; + + // Generic overloads with differing arity tagging with arguments matching each overload type parameter count + var s = fn3 `${ 4 }` + var s = fn3 `${ '' }${ '' }${ '' }`; + var n = fn3 `${ '' }${ '' }${ 3 }`; + + // Generic overloads with differing arity tagging with argument count that doesn't match any overload + fn3 ``; // Error + ~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + + // Generic overloads with constraints + function fn4(strs: TemplateStringsArray, n: T, m: U); + function fn4(strs: TemplateStringsArray, n: T, m: U); + function fn4(strs: TemplateStringsArray) + function fn4() { } + + // Generic overloads with constraints tagged with types that satisfy the constraints + fn4 `${ '' }${ 3 }`; + fn4 `${ 3 }${ '' }`; + fn4 `${ 3 }${ undefined }`; + fn4 `${ '' }${ null }`; + + // Generic overloads with constraints called with type arguments that do not satisfy the constraints + fn4 `${ null }${ null }`; // Error + + // Generic overloads with constraints called without type arguments but with types that do not satisfy the constraints + fn4 `${ true }${ null }`; + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number'. + fn4 `${ null }${ true }`; + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + + // Non - generic overloads where contextual typing of function arguments has errors + function fn5(strs: TemplateStringsArray, f: (n: string) => void): string; + function fn5(strs: TemplateStringsArray, f: (n: number) => void): number; + function fn5() { return undefined; } + fn5 `${ (n) => n.toFixed() }`; // will error; 'n' should have type 'string'. + ~~~~~~~ +!!! error TS2339: Property 'toFixed' does not exist on type 'string'. + fn5 `${ (n) => n.substr(0) }`; + + \ No newline at end of file diff --git a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js new file mode 100644 index 00000000000..48af9403a79 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js @@ -0,0 +1,122 @@ +//// [taggedTemplateStringsWithOverloadResolution3_ES6.ts] +// Ambiguous call picks the first overload in declaration order +function fn1(strs: TemplateStringsArray, s: string): string; +function fn1(strs: TemplateStringsArray, n: number): number; +function fn1() { return null; } + +var s: string = fn1 `${ undefined }`; + +// No candidate overloads found +fn1 `${ {} }`; // Error + +function fn2(strs: TemplateStringsArray, s: string, n: number): number; +function fn2(strs: TemplateStringsArray, n: number, t: T): T; +function fn2() { return undefined; } + +var d1: Date = fn2 `${ 0 }${ undefined }`; // contextually typed +var d2 = fn2 `${ 0 }${ undefined }`; // any + +d1.foo(); // error +d2(); // no error (typed as any) + +// Generic and non-generic overload where generic overload is the only candidate +fn2 `${ 0 }${ '' }`; // OK + +// Generic and non-generic overload where non-generic overload is the only candidate +fn2 `${ '' }${ 0 }`; // OK + +// Generic overloads with differing arity +function fn3(strs: TemplateStringsArray, n: T): string; +function fn3(strs: TemplateStringsArray, s: string, t: T, u: U): U; +function fn3(strs: TemplateStringsArray, v: V, u: U, t: T): number; +function fn3() { return null; } + +var s = fn3 `${ 3 }`; +var s = fn3 `${'' }${ 3 }${ '' }`; +var n = fn3 `${ 5 }${ 5 }${ 5 }`; +var n: number; + +// Generic overloads with differing arity tagging with arguments matching each overload type parameter count +var s = fn3 `${ 4 }` +var s = fn3 `${ '' }${ '' }${ '' }`; +var n = fn3 `${ '' }${ '' }${ 3 }`; + +// Generic overloads with differing arity tagging with argument count that doesn't match any overload +fn3 ``; // Error + +// Generic overloads with constraints +function fn4(strs: TemplateStringsArray, n: T, m: U); +function fn4(strs: TemplateStringsArray, n: T, m: U); +function fn4(strs: TemplateStringsArray) +function fn4() { } + +// Generic overloads with constraints tagged with types that satisfy the constraints +fn4 `${ '' }${ 3 }`; +fn4 `${ 3 }${ '' }`; +fn4 `${ 3 }${ undefined }`; +fn4 `${ '' }${ null }`; + +// Generic overloads with constraints called with type arguments that do not satisfy the constraints +fn4 `${ null }${ null }`; // Error + +// Generic overloads with constraints called without type arguments but with types that do not satisfy the constraints +fn4 `${ true }${ null }`; +fn4 `${ null }${ true }`; + +// Non - generic overloads where contextual typing of function arguments has errors +function fn5(strs: TemplateStringsArray, f: (n: string) => void): string; +function fn5(strs: TemplateStringsArray, f: (n: number) => void): number; +function fn5() { return undefined; } +fn5 `${ (n) => n.toFixed() }`; // will error; 'n' should have type 'string'. +fn5 `${ (n) => n.substr(0) }`; + + + +//// [taggedTemplateStringsWithOverloadResolution3_ES6.js] +function fn1() { + return null; +} +var s = fn1 `${undefined}`; +// No candidate overloads found +fn1 `${{}}`; // Error +function fn2() { + return undefined; +} +var d1 = fn2 `${0}${undefined}`; // contextually typed +var d2 = fn2 `${0}${undefined}`; // any +d1.foo(); // error +d2(); // no error (typed as any) +// Generic and non-generic overload where generic overload is the only candidate +fn2 `${0}${''}`; // OK +// Generic and non-generic overload where non-generic overload is the only candidate +fn2 `${''}${0}`; // OK +function fn3() { + return null; +} +var s = fn3 `${3}`; +var s = fn3 `${''}${3}${''}`; +var n = fn3 `${5}${5}${5}`; +var n; +// Generic overloads with differing arity tagging with arguments matching each overload type parameter count +var s = fn3 `${4}`; +var s = fn3 `${''}${''}${''}`; +var n = fn3 `${''}${''}${3}`; +// Generic overloads with differing arity tagging with argument count that doesn't match any overload +fn3 ``; // Error +function fn4() { +} +// Generic overloads with constraints tagged with types that satisfy the constraints +fn4 `${''}${3}`; +fn4 `${3}${''}`; +fn4 `${3}${undefined}`; +fn4 `${''}${null}`; +// Generic overloads with constraints called with type arguments that do not satisfy the constraints +fn4 `${null}${null}`; // Error +// Generic overloads with constraints called without type arguments but with types that do not satisfy the constraints +fn4 `${true}${null}`; +fn4 `${null}${true}`; +function fn5() { + return undefined; +} +fn5 `${function (n) { return n.toFixed(); }}`; // will error; 'n' should have type 'string'. +fn5 `${function (n) { return n.substr(0); }}`; diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts new file mode 100644 index 00000000000..961544f309a --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts @@ -0,0 +1,92 @@ + + +// Generic tag with one parameter +function noParams(n: T) { } +noParams ``; + +// Generic tag with parameter which does not use type parameter +function noGenericParams(n: string[]) { } +noGenericParams ``; + +// Generic tag with multiple type parameters and only one used in parameter type annotation +function someGenerics1a(n: T, m: number) { } +someGenerics1a `${3}`; + +function someGenerics1b(n: string[], m: U) { } +someGenerics1b `${3}`; + +// Generic tag with argument of function type whose parameter is of type parameter type +function someGenerics2a(strs: string[], n: (x: T) => void) { } +someGenerics2a `${(n: string) => n}`; + +function someGenerics2b(strs: string[], n: (x: T, y: U) => void) { } +someGenerics2b `${ (n: string, x: number) => n }`; + +// Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter +function someGenerics3(strs: string[], producer: () => T) { } +someGenerics3 `${() => ''}`; +someGenerics3 `${() => undefined}`; +someGenerics3 `${() => 3}`; + +// 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type +function someGenerics4(strs: string[], n: T, f: (x: U) => void) { } +someGenerics4 `${4}${ () => null }`; +someGenerics4 `${''}${ () => 3 }`; +someGenerics4 `${ null }${ null }`; + +// 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type +function someGenerics5(strs: string[], n: T, f: (x: U) => void) { } +someGenerics5 `${ 4 } ${ () => null }`; +someGenerics5 `${ '' }${ () => 3 }`; +someGenerics5 `${null}${null}`; + +// Generic tag with multiple arguments of function types that each have parameters of the same generic type +function someGenerics6(strs: string[], a: (a: A) => A, b: (b: A) => A, c: (c: A) => A) { } +someGenerics6 `${ n => n }${ n => n}${ n => n}`; +someGenerics6 `${ n => n }${ n => n}${ n => n}`; +someGenerics6 `${ (n: number) => n }${ (n: number) => n }${ (n: number) => n }`; + +// Generic tag with multiple arguments of function types that each have parameters of different generic type +function someGenerics7(strs: string[], a: (a: A) => A, b: (b: B) => B, c: (c: C) => C) { } +someGenerics7 `${ n => n }${ n => n }${ n => n }`; +someGenerics7 `${ n => n }${ n => n }${ n => n }`; +someGenerics7 `${(n: number) => n}${ (n: string) => n}${ (n: number) => n}`; + +// Generic tag with argument of generic function type +function someGenerics8(strs: string[], n: T): T { return n; } +var x = someGenerics8 `${ someGenerics7 }`; +x `${null}${null}${null}`; + +// Generic tag with multiple parameters of generic type passed arguments with no best common type +function someGenerics9(strs: string[], a: T, b: T, c: T): T { + return null; +} +var a9a = someGenerics9 `${ '' }${ 0 }${ [] }`; +var a9a: {}; + +// Generic tag with multiple parameters of generic type passed arguments with multiple best common types +interface A91 { + x: number; + y?: string; +} +interface A92 { + x: number; + z?: Date; +} + +var a9e = someGenerics9 `${ undefined }${ { x: 6, z: new Date() } }${ { x: 6, y: '' } }`; +var a9e: {}; + +// Generic tag with multiple parameters of generic type passed arguments with a single best common type +var a9d = someGenerics9 `${ { x: 3 }}${ { x: 6 }}${ { x: 6 } }`; +var a9d: { x: number; }; + +// Generic tag with multiple parameters of generic type where one argument is of type 'any' +var anyVar: any; +var a = someGenerics9 `${ 7 }${ anyVar }${ 4 }`; +var a: any; + +// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any' +var arr = someGenerics9 `${ [] }${ null }${ undefined }`; +var arr: any[]; + diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInferenceES6.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInferenceES6.ts new file mode 100644 index 00000000000..bc9fd75cac9 --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInferenceES6.ts @@ -0,0 +1,92 @@ +//@target: es6 + +// Generic tag with one parameter +function noParams(n: T) { } +noParams ``; + +// Generic tag with parameter which does not use type parameter +function noGenericParams(n: string[]) { } +noGenericParams ``; + +// Generic tag with multiple type parameters and only one used in parameter type annotation +function someGenerics1a(n: T, m: number) { } +someGenerics1a `${3}`; + +function someGenerics1b(n: string[], m: U) { } +someGenerics1b `${3}`; + +// Generic tag with argument of function type whose parameter is of type parameter type +function someGenerics2a(strs: string[], n: (x: T) => void) { } +someGenerics2a `${(n: string) => n}`; + +function someGenerics2b(strs: string[], n: (x: T, y: U) => void) { } +someGenerics2b `${ (n: string, x: number) => n }`; + +// Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter +function someGenerics3(strs: string[], producer: () => T) { } +someGenerics3 `${() => ''}`; +someGenerics3 `${() => undefined}`; +someGenerics3 `${() => 3}`; + +// 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type +function someGenerics4(strs: string[], n: T, f: (x: U) => void) { } +someGenerics4 `${4}${ () => null }`; +someGenerics4 `${''}${ () => 3 }`; +someGenerics4 `${ null }${ null }`; + +// 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type +function someGenerics5(strs: string[], n: T, f: (x: U) => void) { } +someGenerics5 `${ 4 } ${ () => null }`; +someGenerics5 `${ '' }${ () => 3 }`; +someGenerics5 `${null}${null}`; + +// Generic tag with multiple arguments of function types that each have parameters of the same generic type +function someGenerics6(strs: string[], a: (a: A) => A, b: (b: A) => A, c: (c: A) => A) { } +someGenerics6 `${ n => n }${ n => n}${ n => n}`; +someGenerics6 `${ n => n }${ n => n}${ n => n}`; +someGenerics6 `${ (n: number) => n }${ (n: number) => n }${ (n: number) => n }`; + +// Generic tag with multiple arguments of function types that each have parameters of different generic type +function someGenerics7(strs: string[], a: (a: A) => A, b: (b: B) => B, c: (c: C) => C) { } +someGenerics7 `${ n => n }${ n => n }${ n => n }`; +someGenerics7 `${ n => n }${ n => n }${ n => n }`; +someGenerics7 `${(n: number) => n}${ (n: string) => n}${ (n: number) => n}`; + +// Generic tag with argument of generic function type +function someGenerics8(strs: string[], n: T): T { return n; } +var x = someGenerics8 `${ someGenerics7 }`; +x `${null}${null}${null}`; + +// Generic tag with multiple parameters of generic type passed arguments with no best common type +function someGenerics9(strs: string[], a: T, b: T, c: T): T { + return null; +} +var a9a = someGenerics9 `${ '' }${ 0 }${ [] }`; +var a9a: {}; + +// Generic tag with multiple parameters of generic type passed arguments with multiple best common types +interface A91 { + x: number; + y?: string; +} +interface A92 { + x: number; + z?: Date; +} + +var a9e = someGenerics9 `${ undefined }${ { x: 6, z: new Date() } }${ { x: 6, y: '' } }`; +var a9e: {}; + +// Generic tag with multiple parameters of generic type passed arguments with a single best common type +var a9d = someGenerics9 `${ { x: 3 }}${ { x: 6 }}${ { x: 6 } }`; +var a9d: { x: number; }; + +// Generic tag with multiple parameters of generic type where one argument is of type 'any' +var anyVar: any; +var a = someGenerics9 `${ 7 }${ anyVar }${ 4 }`; +var a: any; + +// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any' +var arr = someGenerics9 `${ [] }${ null }${ undefined }`; +var arr: any[]; + diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2.ts new file mode 100644 index 00000000000..0ed9b86128b --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2.ts @@ -0,0 +1,18 @@ + +function foo1(strs: TemplateStringsArray, x: number): string; +function foo1(strs: string[], x: number): number; +function foo1(...stuff: any[]): any { + return undefined; +} + +var a = foo1 `${1}`; // string +var b = foo1([], 1); // number + +function foo2(strs: string[], x: number): number; +function foo2(strs: TemplateStringsArray, x: number): string; +function foo2(...stuff: any[]): any { + return undefined; +} + +var c = foo2 `${1}`; // number +var d = foo2([], 1); // number \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2_ES6.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2_ES6.ts new file mode 100644 index 00000000000..6c67de325e2 --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution2_ES6.ts @@ -0,0 +1,18 @@ +//@target: es6 +function foo1(strs: TemplateStringsArray, x: number): string; +function foo1(strs: string[], x: number): number; +function foo1(...stuff: any[]): any { + return undefined; +} + +var a = foo1 `${1}`; // string +var b = foo1([], 1); // number + +function foo2(strs: string[], x: number): number; +function foo2(strs: TemplateStringsArray, x: number): string; +function foo2(...stuff: any[]): any { + return undefined; +} + +var c = foo2 `${1}`; // number +var d = foo2([], 1); // number \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts new file mode 100644 index 00000000000..c390e92de56 --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3.ts @@ -0,0 +1,72 @@ + +// Ambiguous call picks the first overload in declaration order +function fn1(strs: TemplateStringsArray, s: string): string; +function fn1(strs: TemplateStringsArray, n: number): number; +function fn1() { return null; } + +var s: string = fn1 `${ undefined }`; + +// No candidate overloads found +fn1 `${ {} }`; // Error + +function fn2(strs: TemplateStringsArray, s: string, n: number): number; +function fn2(strs: TemplateStringsArray, n: number, t: T): T; +function fn2() { return undefined; } + +var d1: Date = fn2 `${ 0 }${ undefined }`; // contextually typed +var d2 = fn2 `${ 0 }${ undefined }`; // any + +d1.foo(); // error +d2(); // no error (typed as any) + +// Generic and non-generic overload where generic overload is the only candidate +fn2 `${ 0 }${ '' }`; // OK + +// Generic and non-generic overload where non-generic overload is the only candidate +fn2 `${ '' }${ 0 }`; // OK + +// Generic overloads with differing arity +function fn3(strs: TemplateStringsArray, n: T): string; +function fn3(strs: TemplateStringsArray, s: string, t: T, u: U): U; +function fn3(strs: TemplateStringsArray, v: V, u: U, t: T): number; +function fn3() { return null; } + +var s = fn3 `${ 3 }`; +var s = fn3 `${'' }${ 3 }${ '' }`; +var n = fn3 `${ 5 }${ 5 }${ 5 }`; +var n: number; + +// Generic overloads with differing arity tagging with arguments matching each overload type parameter count +var s = fn3 `${ 4 }` +var s = fn3 `${ '' }${ '' }${ '' }`; +var n = fn3 `${ '' }${ '' }${ 3 }`; + +// Generic overloads with differing arity tagging with argument count that doesn't match any overload +fn3 ``; // Error + +// Generic overloads with constraints +function fn4(strs: TemplateStringsArray, n: T, m: U); +function fn4(strs: TemplateStringsArray, n: T, m: U); +function fn4(strs: TemplateStringsArray) +function fn4() { } + +// Generic overloads with constraints tagged with types that satisfy the constraints +fn4 `${ '' }${ 3 }`; +fn4 `${ 3 }${ '' }`; +fn4 `${ 3 }${ undefined }`; +fn4 `${ '' }${ null }`; + +// Generic overloads with constraints called with type arguments that do not satisfy the constraints +fn4 `${ null }${ null }`; // Error + +// Generic overloads with constraints called without type arguments but with types that do not satisfy the constraints +fn4 `${ true }${ null }`; +fn4 `${ null }${ true }`; + +// Non - generic overloads where contextual typing of function arguments has errors +function fn5(strs: TemplateStringsArray, f: (n: string) => void): string; +function fn5(strs: TemplateStringsArray, f: (n: number) => void): number; +function fn5() { return undefined; } +fn5 `${ (n) => n.toFixed() }`; // will error; 'n' should have type 'string'. +fn5 `${ (n) => n.substr(0) }`; + diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts new file mode 100644 index 00000000000..a3267e7f0ae --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithOverloadResolution3_ES6.ts @@ -0,0 +1,72 @@ +//@target: es6 +// Ambiguous call picks the first overload in declaration order +function fn1(strs: TemplateStringsArray, s: string): string; +function fn1(strs: TemplateStringsArray, n: number): number; +function fn1() { return null; } + +var s: string = fn1 `${ undefined }`; + +// No candidate overloads found +fn1 `${ {} }`; // Error + +function fn2(strs: TemplateStringsArray, s: string, n: number): number; +function fn2(strs: TemplateStringsArray, n: number, t: T): T; +function fn2() { return undefined; } + +var d1: Date = fn2 `${ 0 }${ undefined }`; // contextually typed +var d2 = fn2 `${ 0 }${ undefined }`; // any + +d1.foo(); // error +d2(); // no error (typed as any) + +// Generic and non-generic overload where generic overload is the only candidate +fn2 `${ 0 }${ '' }`; // OK + +// Generic and non-generic overload where non-generic overload is the only candidate +fn2 `${ '' }${ 0 }`; // OK + +// Generic overloads with differing arity +function fn3(strs: TemplateStringsArray, n: T): string; +function fn3(strs: TemplateStringsArray, s: string, t: T, u: U): U; +function fn3(strs: TemplateStringsArray, v: V, u: U, t: T): number; +function fn3() { return null; } + +var s = fn3 `${ 3 }`; +var s = fn3 `${'' }${ 3 }${ '' }`; +var n = fn3 `${ 5 }${ 5 }${ 5 }`; +var n: number; + +// Generic overloads with differing arity tagging with arguments matching each overload type parameter count +var s = fn3 `${ 4 }` +var s = fn3 `${ '' }${ '' }${ '' }`; +var n = fn3 `${ '' }${ '' }${ 3 }`; + +// Generic overloads with differing arity tagging with argument count that doesn't match any overload +fn3 ``; // Error + +// Generic overloads with constraints +function fn4(strs: TemplateStringsArray, n: T, m: U); +function fn4(strs: TemplateStringsArray, n: T, m: U); +function fn4(strs: TemplateStringsArray) +function fn4() { } + +// Generic overloads with constraints tagged with types that satisfy the constraints +fn4 `${ '' }${ 3 }`; +fn4 `${ 3 }${ '' }`; +fn4 `${ 3 }${ undefined }`; +fn4 `${ '' }${ null }`; + +// Generic overloads with constraints called with type arguments that do not satisfy the constraints +fn4 `${ null }${ null }`; // Error + +// Generic overloads with constraints called without type arguments but with types that do not satisfy the constraints +fn4 `${ true }${ null }`; +fn4 `${ null }${ true }`; + +// Non - generic overloads where contextual typing of function arguments has errors +function fn5(strs: TemplateStringsArray, f: (n: string) => void): string; +function fn5(strs: TemplateStringsArray, f: (n: number) => void): number; +function fn5() { return undefined; } +fn5 `${ (n) => n.toFixed() }`; // will error; 'n' should have type 'string'. +fn5 `${ (n) => n.substr(0) }`; +