diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 37a1135d2d4..eb00c3708d1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -28724,8 +28724,10 @@ namespace ts { * Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise. */ function getThisArgumentOfCall(node: CallLikeExpression): LeftHandSideExpression | undefined { - if (node.kind === SyntaxKind.CallExpression) { - const callee = skipOuterExpressions(node.expression); + const expression = node.kind === SyntaxKind.CallExpression ? node.expression : + node.kind === SyntaxKind.TaggedTemplateExpression ? node.tag : undefined; + if (expression) { + const callee = skipOuterExpressions(expression); if (isAccessExpression(callee)) { return callee.expression; } diff --git a/tests/baselines/reference/thisTypeInTaggedTemplateCall.js b/tests/baselines/reference/thisTypeInTaggedTemplateCall.js new file mode 100644 index 00000000000..de5c21307cf --- /dev/null +++ b/tests/baselines/reference/thisTypeInTaggedTemplateCall.js @@ -0,0 +1,19 @@ +//// [thisTypeInTaggedTemplateCall.ts] +class Foo { + static m(this: new () => T, strings: TemplateStringsArray | string) { + return new this() + } +} + +Foo.m`test`; +(Foo.m)`test`; + + +//// [thisTypeInTaggedTemplateCall.js] +class Foo { + static m(strings) { + return new this(); + } +} +Foo.m `test`; +(Foo.m) `test`; diff --git a/tests/baselines/reference/thisTypeInTaggedTemplateCall.symbols b/tests/baselines/reference/thisTypeInTaggedTemplateCall.symbols new file mode 100644 index 00000000000..4bb013c4da1 --- /dev/null +++ b/tests/baselines/reference/thisTypeInTaggedTemplateCall.symbols @@ -0,0 +1,27 @@ +=== tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts === +class Foo { +>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0)) + + static m(this: new () => T, strings: TemplateStringsArray | string) { +>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11)) +>T : Symbol(T, Decl(thisTypeInTaggedTemplateCall.ts, 1, 13)) +>this : Symbol(this, Decl(thisTypeInTaggedTemplateCall.ts, 1, 16)) +>T : Symbol(T, Decl(thisTypeInTaggedTemplateCall.ts, 1, 13)) +>strings : Symbol(strings, Decl(thisTypeInTaggedTemplateCall.ts, 1, 34)) +>TemplateStringsArray : Symbol(TemplateStringsArray, Decl(lib.es5.d.ts, --, --)) + + return new this() +>this : Symbol(this, Decl(thisTypeInTaggedTemplateCall.ts, 1, 16)) + } +} + +Foo.m`test`; +>Foo.m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11)) +>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0)) +>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11)) + +(Foo.m)`test`; +>Foo.m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11)) +>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0)) +>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11)) + diff --git a/tests/baselines/reference/thisTypeInTaggedTemplateCall.types b/tests/baselines/reference/thisTypeInTaggedTemplateCall.types new file mode 100644 index 00000000000..4c64229924e --- /dev/null +++ b/tests/baselines/reference/thisTypeInTaggedTemplateCall.types @@ -0,0 +1,30 @@ +=== tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts === +class Foo { +>Foo : Foo + + static m(this: new () => T, strings: TemplateStringsArray | string) { +>m : (this: new () => T, strings: TemplateStringsArray | string) => T +>this : new () => T +>strings : string | TemplateStringsArray + + return new this() +>new this() : T +>this : new () => T + } +} + +Foo.m`test`; +>Foo.m`test` : Foo +>Foo.m : (this: new () => T, strings: string | TemplateStringsArray) => T +>Foo : typeof Foo +>m : (this: new () => T, strings: string | TemplateStringsArray) => T +>`test` : "test" + +(Foo.m)`test`; +>(Foo.m)`test` : Foo +>(Foo.m) : (this: new () => T, strings: string | TemplateStringsArray) => T +>Foo.m : (this: new () => T, strings: string | TemplateStringsArray) => T +>Foo : typeof Foo +>m : (this: new () => T, strings: string | TemplateStringsArray) => T +>`test` : "test" + diff --git a/tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts b/tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts new file mode 100644 index 00000000000..c46933329b6 --- /dev/null +++ b/tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts @@ -0,0 +1,10 @@ +// @target: esnext + +class Foo { + static m(this: new () => T, strings: TemplateStringsArray | string) { + return new this() + } +} + +Foo.m`test`; +(Foo.m)`test`;