diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 889b387d4f4..b0c0305670e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4813,17 +4813,25 @@ module ts { return undefined; } - // In a typed function call, an argument expression is contextually typed by the type of the corresponding parameter. - function getContextualTypeForArgument(node: Expression): Type { - var callExpression = node.parent; - var argIndex = indexOf(callExpression.arguments, node); + // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. + function getContextualTypeForArgument(callTarget: CallLikeExpression, arg: Expression): Type { + var args = getEffectiveCallArguments(callTarget); + var argIndex = indexOf(args, arg); if (argIndex >= 0) { - var signature = getResolvedSignature(callExpression); + var signature = getResolvedSignature(callTarget); return getTypeAtPosition(signature, argIndex); } return undefined; } + function getContextualTypeForSubstitutionExpression(template: TemplateExpression, substitutionExpression: Expression) { + if (template.parent.kind === SyntaxKind.TaggedTemplateExpression) { + return getContextualTypeForArgument(template.parent, substitutionExpression); + } + + return undefined; + } + function getContextualTypeForBinaryOperand(node: Expression): Type { var binaryExpression = node.parent; var operator = binaryExpression.operator; @@ -4959,7 +4967,7 @@ module ts { return getContextualTypeForReturnExpression(node); case SyntaxKind.CallExpression: case SyntaxKind.NewExpression: - return getContextualTypeForArgument(node); + return getContextualTypeForArgument(parent, node); case SyntaxKind.TypeAssertionExpression: return getTypeFromTypeNode((parent).type); case SyntaxKind.BinaryExpression: @@ -4970,6 +4978,9 @@ module ts { return getContextualTypeForElementExpression(node); case SyntaxKind.ConditionalExpression: return getContextualTypeForConditionalOperand(node); + case SyntaxKind.TemplateSpan: + Debug.assert(parent.parent.kind === SyntaxKind.TemplateExpression); + return getContextualTypeForSubstitutionExpression(parent.parent, node); } return undefined; } @@ -5571,7 +5582,7 @@ module ts { } /** - * Returns the effective arguments for an expression that works like a function invokation. + * Returns the effective arguments for an expression that works like a function invocation. * * If 'node' is a CallExpression or a NewExpression, then its argument list is returned. * If 'node' is a TaggedTemplateExpression, a new argument list is constructed from the substitution @@ -8636,6 +8647,8 @@ module ts { case SyntaxKind.CallExpression: case SyntaxKind.NewExpression: case SyntaxKind.TaggedTemplateExpression: + case SyntaxKind.TemplateExpression: + case SyntaxKind.TemplateSpan: case SyntaxKind.TypeAssertionExpression: case SyntaxKind.ParenthesizedExpression: case SyntaxKind.TypeOfExpression: diff --git a/tests/baselines/reference/taggedTemplateContextualTyping1.js b/tests/baselines/reference/taggedTemplateContextualTyping1.js new file mode 100644 index 00000000000..1d8b79e4c32 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateContextualTyping1.js @@ -0,0 +1,52 @@ +//// [taggedTemplateContextualTyping1.ts] + +type FuncType = (x: (p: T) => T) => typeof x; + +function tempTag1(templateStrs: TemplateStringsArray, f: FuncType, x: T): T; +function tempTag1(templateStrs: TemplateStringsArray, f: FuncType, h: FuncType, x: T): T; +function tempTag1(...rest: any[]): T { + return undefined; +} + +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag1 `${ x => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ 10 }`; +tempTag1 `${ x => { x(undefined); return x; } }${ (y: (p: T) => T) => { y(undefined); return y } }${ undefined }`; +tempTag1 `${ (x: (p: T) => T) => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ undefined }`; + + +//// [taggedTemplateContextualTyping1.js] +function tempTag1() { + var rest = []; + for (var _i = 0; _i < arguments.length; _i++) { + rest[_i - 0] = arguments[_i]; + } + return undefined; +} +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag1 `${function (x) { + x(undefined); + return x; +}}${function (y) { + y(undefined); + return y; +}}${10}`; +tempTag1 `${function (x) { + x(undefined); + return x; +}}${function (y) { + y(undefined); + return y; +}}${undefined}`; +tempTag1 `${function (x) { + x(undefined); + return x; +}}${function (y) { + y(undefined); + return y; +}}${undefined}`; diff --git a/tests/baselines/reference/taggedTemplateContextualTyping1.types b/tests/baselines/reference/taggedTemplateContextualTyping1.types new file mode 100644 index 00000000000..a87d5eaa7a4 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateContextualTyping1.types @@ -0,0 +1,104 @@ +=== tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts === + +type FuncType = (x: (p: T) => T) => typeof x; +>FuncType : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>T : T +>p : T +>T : T +>T : T +>x : (p: T) => T + +function tempTag1(templateStrs: TemplateStringsArray, f: FuncType, x: T): T; +>tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } +>T : T +>templateStrs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>f : (x: (p: T) => T) => (p: T) => T +>FuncType : (x: (p: T) => T) => (p: T) => T +>x : T +>T : T +>T : T + +function tempTag1(templateStrs: TemplateStringsArray, f: FuncType, h: FuncType, x: T): T; +>tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } +>T : T +>templateStrs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>f : (x: (p: T) => T) => (p: T) => T +>FuncType : (x: (p: T) => T) => (p: T) => T +>h : (x: (p: T) => T) => (p: T) => T +>FuncType : (x: (p: T) => T) => (p: T) => T +>x : T +>T : T +>T : T + +function tempTag1(...rest: any[]): T { +>tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } +>T : T +>rest : any[] +>T : T + + return undefined; +>undefined : undefined +} + +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag1 `${ x => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ 10 }`; +>tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } +>x => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>x(undefined) : number +>x : (p: T) => T +>undefined : undefined +>x : (p: T) => T +>y => { y(undefined); return y; } : (y: (p: T) => T) => (p: T) => T +>y : (p: T) => T +>y(undefined) : number +>y : (p: T) => T +>undefined : undefined +>y : (p: T) => T + +tempTag1 `${ x => { x(undefined); return x; } }${ (y: (p: T) => T) => { y(undefined); return y } }${ undefined }`; +>tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } +>x => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>x(undefined) : number +>x : (p: T) => T +>undefined : undefined +>x : (p: T) => T +>(y: (p: T) => T) => { y(undefined); return y } : (y: (p: T) => T) => (p: T) => T +>y : (p: T) => T +>T : T +>p : T +>T : T +>T : T +>y(undefined) : number +>y : (p: T) => T +>undefined : undefined +>y : (p: T) => T +>undefined : undefined + +tempTag1 `${ (x: (p: T) => T) => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ undefined }`; +>tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } +>(x: (p: T) => T) => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>T : T +>p : T +>T : T +>T : T +>x(undefined) : number +>x : (p: T) => T +>undefined : undefined +>x : (p: T) => T +>y => { y(undefined); return y; } : (y: (p: T) => T) => (p: T) => T +>y : (p: T) => T +>y(undefined) : number +>y : (p: T) => T +>undefined : undefined +>y : (p: T) => T +>undefined : undefined + diff --git a/tests/baselines/reference/taggedTemplateContextualTyping2.js b/tests/baselines/reference/taggedTemplateContextualTyping2.js new file mode 100644 index 00000000000..df026399c10 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateContextualTyping2.js @@ -0,0 +1,46 @@ +//// [taggedTemplateContextualTyping2.ts] + +type FuncType1 = (x: (p: T) => T) => typeof x; +type FuncType2 = (x: (p: T) => T) => typeof x; + +function tempTag2(templateStrs: TemplateStringsArray, f: FuncType1, x: number): number; +function tempTag2(templateStrs: TemplateStringsArray, f: FuncType2, h: FuncType2, x: string): string; +function tempTag2(...rest: any[]): any { + return undefined; +} + +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag2 `${ x => { x(undefined); return x; } }${ 0 }`; +tempTag2 `${ x => { x(undefined); return x; } }${ y => { y(null); return y; } }${ "hello" }`; +tempTag2 `${ x => { x(undefined); return x; } }${ undefined }${ "hello" }`; + +//// [taggedTemplateContextualTyping2.js] +function tempTag2() { + var rest = []; + for (var _i = 0; _i < arguments.length; _i++) { + rest[_i - 0] = arguments[_i]; + } + return undefined; +} +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag2 `${function (x) { + x(undefined); + return x; +}}${0}`; +tempTag2 `${function (x) { + x(undefined); + return x; +}}${function (y) { + y(null); + return y; +}}${"hello"}`; +tempTag2 `${function (x) { + x(undefined); + return x; +}}${undefined}${"hello"}`; diff --git a/tests/baselines/reference/taggedTemplateContextualTyping2.types b/tests/baselines/reference/taggedTemplateContextualTyping2.types new file mode 100644 index 00000000000..9ca7386dc34 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateContextualTyping2.types @@ -0,0 +1,84 @@ +=== tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping2.ts === + +type FuncType1 = (x: (p: T) => T) => typeof x; +>FuncType1 : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>T : T +>p : T +>T : T +>T : T +>x : (p: T) => T + +type FuncType2 = (x: (p: T) => T) => typeof x; +>FuncType2 : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>S : S +>T : T +>p : T +>T : T +>T : T +>x : (p: T) => T + +function tempTag2(templateStrs: TemplateStringsArray, f: FuncType1, x: number): number; +>tempTag2 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: number): number; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: string): string; } +>templateStrs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>f : (x: (p: T) => T) => (p: T) => T +>FuncType1 : (x: (p: T) => T) => (p: T) => T +>x : number + +function tempTag2(templateStrs: TemplateStringsArray, f: FuncType2, h: FuncType2, x: string): string; +>tempTag2 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: number): number; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: string): string; } +>templateStrs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>f : (x: (p: T) => T) => (p: T) => T +>FuncType2 : (x: (p: T) => T) => (p: T) => T +>h : (x: (p: T) => T) => (p: T) => T +>FuncType2 : (x: (p: T) => T) => (p: T) => T +>x : string + +function tempTag2(...rest: any[]): any { +>tempTag2 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: number): number; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: string): string; } +>rest : any[] + + return undefined; +>undefined : undefined +} + +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag2 `${ x => { x(undefined); return x; } }${ 0 }`; +>tempTag2 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: number): number; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: string): string; } +>x => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>x(undefined) : number +>x : (p: T) => T +>undefined : undefined +>x : (p: T) => T + +tempTag2 `${ x => { x(undefined); return x; } }${ y => { y(null); return y; } }${ "hello" }`; +>tempTag2 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: number): number; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: string): string; } +>x => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>x(undefined) : string +>x : (p: T) => T +>undefined : undefined +>x : (p: T) => T +>y => { y(null); return y; } : (y: (p: T) => T) => (p: T) => T +>y : (p: T) => T +>y(null) : number +>y : (p: T) => T +>y : (p: T) => T + +tempTag2 `${ x => { x(undefined); return x; } }${ undefined }${ "hello" }`; +>tempTag2 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: number): number; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: string): string; } +>x => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>x(undefined) : string +>x : (p: T) => T +>undefined : undefined +>x : (p: T) => T +>undefined : undefined + diff --git a/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.errors.txt b/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.errors.txt new file mode 100644 index 00000000000..b38a70ca18b --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.errors.txt @@ -0,0 +1,15 @@ +tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts(6,5): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts(6,31): error TS2322: Type 'string' is not assignable to type 'number'. + + +==== tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts (2 errors) ==== + + + function foo(...rest: any[]) { + } + + foo `${function (x: number) { x = "bad"; } }`; + ~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.errors.txt b/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.errors.txt new file mode 100644 index 00000000000..6b0f4b642ff --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts(5,31): error TS2322: Type 'string' is not assignable to type 'number'. + + +==== tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts (1 errors) ==== + + function foo(...rest: any[]) { + } + + foo `${function (x: number) { x = "bad"; } }`; + ~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js b/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js new file mode 100644 index 00000000000..a1d3479f5c2 --- /dev/null +++ b/tests/baselines/reference/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js @@ -0,0 +1,17 @@ +//// [taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts] + +function foo(...rest: any[]) { +} + +foo `${function (x: number) { x = "bad"; } }`; + +//// [taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js] +function foo() { + var rest = []; + for (var _i = 0; _i < arguments.length; _i++) { + rest[_i - 0] = arguments[_i]; + } +} +foo `${function (x) { + x = "bad"; +}}`; diff --git a/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.errors.txt b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.errors.txt new file mode 100644 index 00000000000..7204f32dba2 --- /dev/null +++ b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts(3,27): error TS2322: Type 'string' is not assignable to type 'number'. + + +==== tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts (1 errors) ==== + + + `${function (x: number) { x = "bad"; } }`; + ~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.js b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.js new file mode 100644 index 00000000000..ef113312060 --- /dev/null +++ b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.js @@ -0,0 +1,9 @@ +//// [templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts] + + +`${function (x: number) { x = "bad"; } }`; + +//// [templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.js] +"" + function (x) { + x = "bad"; +}; diff --git a/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.errors.txt b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.errors.txt new file mode 100644 index 00000000000..9fece57ef5e --- /dev/null +++ b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.errors.txt @@ -0,0 +1,8 @@ +tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts(2,27): error TS2322: Type 'string' is not assignable to type 'number'. + + +==== tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts (1 errors) ==== + + `${function (x: number) { x = "bad"; } }`; + ~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js new file mode 100644 index 00000000000..9ce47348dc1 --- /dev/null +++ b/tests/baselines/reference/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js @@ -0,0 +1,8 @@ +//// [templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts] + +`${function (x: number) { x = "bad"; } }`; + +//// [templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.js] +`${function (x) { + x = "bad"; +}}`; diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts new file mode 100644 index 00000000000..210f3eebbd7 --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts @@ -0,0 +1,6 @@ + + +function foo(...rest: any[]) { +} + +foo `${function (x: number) { x = "bad"; } }`; \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts new file mode 100644 index 00000000000..0a51fea491f --- /dev/null +++ b/tests/cases/conformance/es6/templates/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts @@ -0,0 +1,6 @@ +//@target: es6 + +function foo(...rest: any[]) { +} + +foo `${function (x: number) { x = "bad"; } }`; \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts b/tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts new file mode 100644 index 00000000000..26995f21e58 --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts @@ -0,0 +1,3 @@ + + +`${function (x: number) { x = "bad"; } }`; \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts b/tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts new file mode 100644 index 00000000000..4803285a24a --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6.ts @@ -0,0 +1,3 @@ +//@target: es6 + +`${function (x: number) { x = "bad"; } }`; \ No newline at end of file diff --git a/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts b/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts new file mode 100644 index 00000000000..c15d911ee52 --- /dev/null +++ b/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts @@ -0,0 +1,17 @@ +// @target: ES6 + +type FuncType = (x: (p: T) => T) => typeof x; + +function tempTag1(templateStrs: TemplateStringsArray, f: FuncType, x: T): T; +function tempTag1(templateStrs: TemplateStringsArray, f: FuncType, h: FuncType, x: T): T; +function tempTag1(...rest: any[]): T { + return undefined; +} + +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag1 `${ x => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ 10 }`; +tempTag1 `${ x => { x(undefined); return x; } }${ (y: (p: T) => T) => { y(undefined); return y } }${ undefined }`; +tempTag1 `${ (x: (p: T) => T) => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ undefined }`; diff --git a/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping2.ts b/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping2.ts new file mode 100644 index 00000000000..24fcf4a04df --- /dev/null +++ b/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping2.ts @@ -0,0 +1,18 @@ +// @target: ES6 + +type FuncType1 = (x: (p: T) => T) => typeof x; +type FuncType2 = (x: (p: T) => T) => typeof x; + +function tempTag2(templateStrs: TemplateStringsArray, f: FuncType1, x: number): number; +function tempTag2(templateStrs: TemplateStringsArray, f: FuncType2, h: FuncType2, x: string): string; +function tempTag2(...rest: any[]): any { + return undefined; +} + +// If contextual typing takes place, these functions should work. +// Otherwise, the arrow functions' parameters will be typed as 'any', +// and it is an error to invoke an any-typed value with type arguments, +// so this test will error. +tempTag2 `${ x => { x(undefined); return x; } }${ 0 }`; +tempTag2 `${ x => { x(undefined); return x; } }${ y => { y(null); return y; } }${ "hello" }`; +tempTag2 `${ x => { x(undefined); return x; } }${ undefined }${ "hello" }`; \ No newline at end of file diff --git a/tests/cases/fourslash/quickInfoForContextuallyTypedFunctionInTaggedTemplateExpression1.ts b/tests/cases/fourslash/quickInfoForContextuallyTypedFunctionInTaggedTemplateExpression1.ts new file mode 100644 index 00000000000..ce6b454843a --- /dev/null +++ b/tests/cases/fourslash/quickInfoForContextuallyTypedFunctionInTaggedTemplateExpression1.ts @@ -0,0 +1,19 @@ +/// + +////function tempTag1(templateStrs: TemplateStringsArray, f: (x: T) => T, x: T): T; +////function tempTag1(templateStrs: TemplateStringsArray, f: (x: T) => T, h: (y: T) => T, x: T): T; +////function tempTag1(...rest: any[]): T { +//// return undefined; +////} +//// +////tempTag1 `${ x => /*0*/x }${ 10 }`; +////tempTag1 `${ x => /*1*/x }${ x => /*2*/x }${ 10 }`; +////tempTag1 `${ x => /*3*/x }${ (x: number) => /*4*/x }${ undefined }`; +////tempTag1 `${ (x: number) => /*5*/x }${ x => /*6*/x }${ undefined }`; + +var markers = test.markers(); + +markers.forEach(marker => { + goTo.position(marker.position); + verify.quickInfoIs("(parameter) x: number"); +}); \ No newline at end of file diff --git a/tests/cases/fourslash/quickInfoForContextuallyTypedFunctionInTaggedTemplateExpression2.ts b/tests/cases/fourslash/quickInfoForContextuallyTypedFunctionInTaggedTemplateExpression2.ts new file mode 100644 index 00000000000..d878d601416 --- /dev/null +++ b/tests/cases/fourslash/quickInfoForContextuallyTypedFunctionInTaggedTemplateExpression2.ts @@ -0,0 +1,31 @@ +/// + +////function tempTag2(templateStrs: TemplateStringsArray, f: (x: number) => number, x: number): number; +////function tempTag2(templateStrs: TemplateStringsArray, f: (x: string) => string, h: (y: string) => string, x: string): string; +////function tempTag2(...rest: any[]): any { +//// return undefined; +////} +//// +////tempTag2 `${ x => /*0*/x }${ 0 }`; +////tempTag2 `${ /*1*/x => /*2*/x }${ undefined }`; +////tempTag2 `${ x => /*3*/x }${ x => /*4*/x }${ "hello" }`; +////tempTag2 `${ x => /*5*/x }${ undefined }${ "hello" }`; + +// The first group of parameters, [0, 2], should all be contextually typed as 'number'. +// The second group, [3, 5], should be typed as 'string'. +var numTypedVariableCount = 3; +var strTypedVariableCount = 3; + +if (numTypedVariableCount + strTypedVariableCount !== test.markers().length) { + throw "Unexpected number of markers in file."; +} + +for (var i = 0; i < numTypedVariableCount; i++) { + goTo.marker("" + i); + verify.quickInfoIs("(parameter) x: number"); +} + +for (var i = 0; i < strTypedVariableCount; i++) { + goTo.marker("" + (i + numTypedVariableCount)); + verify.quickInfoIs("(parameter) x: string"); +} \ No newline at end of file