From 7669bfba159e05bf0e1bc5be53a83abc1a612988 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 2 Aug 2021 12:44:56 -0700 Subject: [PATCH] Actually instantiate the type of the annotation used for contextual types (#45285) --- src/compiler/checker.ts | 2 +- ...eritedConstructorPropertyContextualType.js | 39 +++++++++++++++++++ ...dConstructorPropertyContextualType.symbols | 31 +++++++++++++++ ...tedConstructorPropertyContextualType.types | 30 ++++++++++++++ ...eritedConstructorPropertyContextualType.ts | 12 ++++++ 5 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/inheritedConstructorPropertyContextualType.js create mode 100644 tests/baselines/reference/inheritedConstructorPropertyContextualType.symbols create mode 100644 tests/baselines/reference/inheritedConstructorPropertyContextualType.types create mode 100644 tests/cases/compiler/inheritedConstructorPropertyContextualType.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d65e196ff24..b96d0462e13 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25413,7 +25413,7 @@ namespace ts { // We avoid calling back into `getTypeOfExpression` and reentering contextual typing to avoid a bogus circularity error in that case. if (decl && (isPropertyDeclaration(decl) || isPropertySignature(decl))) { const overallAnnotation = getEffectiveTypeAnnotationNode(decl); - return (overallAnnotation && getTypeFromTypeNode(overallAnnotation)) || + return (overallAnnotation && instantiateType(getTypeFromTypeNode(overallAnnotation), getSymbolLinks(lhsSymbol).mapper)) || (decl.initializer && getTypeOfExpression(binaryExpression.left)); } if (kind === AssignmentDeclarationKind.None) { diff --git a/tests/baselines/reference/inheritedConstructorPropertyContextualType.js b/tests/baselines/reference/inheritedConstructorPropertyContextualType.js new file mode 100644 index 00000000000..53c607cad9b --- /dev/null +++ b/tests/baselines/reference/inheritedConstructorPropertyContextualType.js @@ -0,0 +1,39 @@ +//// [inheritedConstructorPropertyContextualType.ts] +interface State { + version: 2 +} +declare class Base { + state: S +} +class Assignment extends Base { + constructor() { + super() + this.state = { version: 2 } + } +} + +//// [inheritedConstructorPropertyContextualType.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var Assignment = /** @class */ (function (_super) { + __extends(Assignment, _super); + function Assignment() { + var _this = _super.call(this) || this; + _this.state = { version: 2 }; + return _this; + } + return Assignment; +}(Base)); diff --git a/tests/baselines/reference/inheritedConstructorPropertyContextualType.symbols b/tests/baselines/reference/inheritedConstructorPropertyContextualType.symbols new file mode 100644 index 00000000000..073603637d2 --- /dev/null +++ b/tests/baselines/reference/inheritedConstructorPropertyContextualType.symbols @@ -0,0 +1,31 @@ +=== tests/cases/compiler/inheritedConstructorPropertyContextualType.ts === +interface State { +>State : Symbol(State, Decl(inheritedConstructorPropertyContextualType.ts, 0, 0)) + + version: 2 +>version : Symbol(State.version, Decl(inheritedConstructorPropertyContextualType.ts, 0, 17)) +} +declare class Base { +>Base : Symbol(Base, Decl(inheritedConstructorPropertyContextualType.ts, 2, 1)) +>S : Symbol(S, Decl(inheritedConstructorPropertyContextualType.ts, 3, 19)) + + state: S +>state : Symbol(Base.state, Decl(inheritedConstructorPropertyContextualType.ts, 3, 23)) +>S : Symbol(S, Decl(inheritedConstructorPropertyContextualType.ts, 3, 19)) +} +class Assignment extends Base { +>Assignment : Symbol(Assignment, Decl(inheritedConstructorPropertyContextualType.ts, 5, 1)) +>Base : Symbol(Base, Decl(inheritedConstructorPropertyContextualType.ts, 2, 1)) +>State : Symbol(State, Decl(inheritedConstructorPropertyContextualType.ts, 0, 0)) + + constructor() { + super() +>super : Symbol(Base, Decl(inheritedConstructorPropertyContextualType.ts, 2, 1)) + + this.state = { version: 2 } +>this.state : Symbol(Base.state, Decl(inheritedConstructorPropertyContextualType.ts, 3, 23)) +>this : Symbol(Assignment, Decl(inheritedConstructorPropertyContextualType.ts, 5, 1)) +>state : Symbol(Base.state, Decl(inheritedConstructorPropertyContextualType.ts, 3, 23)) +>version : Symbol(version, Decl(inheritedConstructorPropertyContextualType.ts, 9, 22)) + } +} diff --git a/tests/baselines/reference/inheritedConstructorPropertyContextualType.types b/tests/baselines/reference/inheritedConstructorPropertyContextualType.types new file mode 100644 index 00000000000..bf707519fb6 --- /dev/null +++ b/tests/baselines/reference/inheritedConstructorPropertyContextualType.types @@ -0,0 +1,30 @@ +=== tests/cases/compiler/inheritedConstructorPropertyContextualType.ts === +interface State { + version: 2 +>version : 2 +} +declare class Base { +>Base : Base + + state: S +>state : S +} +class Assignment extends Base { +>Assignment : Assignment +>Base : Base + + constructor() { + super() +>super() : void +>super : typeof Base + + this.state = { version: 2 } +>this.state = { version: 2 } : { version: 2; } +>this.state : State +>this : this +>state : State +>{ version: 2 } : { version: 2; } +>version : 2 +>2 : 2 + } +} diff --git a/tests/cases/compiler/inheritedConstructorPropertyContextualType.ts b/tests/cases/compiler/inheritedConstructorPropertyContextualType.ts new file mode 100644 index 00000000000..9b8247d40b3 --- /dev/null +++ b/tests/cases/compiler/inheritedConstructorPropertyContextualType.ts @@ -0,0 +1,12 @@ +interface State { + version: 2 +} +declare class Base { + state: S +} +class Assignment extends Base { + constructor() { + super() + this.state = { version: 2 } + } +} \ No newline at end of file