Actually instantiate the type of the annotation used for contextual types (#45285)

This commit is contained in:
Wesley Wigham 2021-08-02 12:44:56 -07:00 committed by GitHub
parent afe9cf5307
commit 7669bfba15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 113 additions and 1 deletions

View File

@ -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) {

View File

@ -0,0 +1,39 @@
//// [inheritedConstructorPropertyContextualType.ts]
interface State {
version: 2
}
declare class Base<S> {
state: S
}
class Assignment extends Base<State> {
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));

View File

@ -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<S> {
>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<State> {
>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))
}
}

View File

@ -0,0 +1,30 @@
=== tests/cases/compiler/inheritedConstructorPropertyContextualType.ts ===
interface State {
version: 2
>version : 2
}
declare class Base<S> {
>Base : Base<S>
state: S
>state : S
}
class Assignment extends Base<State> {
>Assignment : Assignment
>Base : Base<State>
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
}
}

View File

@ -0,0 +1,12 @@
interface State {
version: 2
}
declare class Base<S> {
state: S
}
class Assignment extends Base<State> {
constructor() {
super()
this.state = { version: 2 }
}
}