Skip to content

Commit 7669bfb

Browse files
authored
Actually instantiate the type of the annotation used for contextual types (#45285)
1 parent afe9cf5 commit 7669bfb

5 files changed

+113
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25413,7 +25413,7 @@ namespace ts {
2541325413
// We avoid calling back into `getTypeOfExpression` and reentering contextual typing to avoid a bogus circularity error in that case.
2541425414
if (decl && (isPropertyDeclaration(decl) || isPropertySignature(decl))) {
2541525415
const overallAnnotation = getEffectiveTypeAnnotationNode(decl);
25416-
return (overallAnnotation && getTypeFromTypeNode(overallAnnotation)) ||
25416+
return (overallAnnotation && instantiateType(getTypeFromTypeNode(overallAnnotation), getSymbolLinks(lhsSymbol).mapper)) ||
2541725417
(decl.initializer && getTypeOfExpression(binaryExpression.left));
2541825418
}
2541925419
if (kind === AssignmentDeclarationKind.None) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//// [inheritedConstructorPropertyContextualType.ts]
2+
interface State {
3+
version: 2
4+
}
5+
declare class Base<S> {
6+
state: S
7+
}
8+
class Assignment extends Base<State> {
9+
constructor() {
10+
super()
11+
this.state = { version: 2 }
12+
}
13+
}
14+
15+
//// [inheritedConstructorPropertyContextualType.js]
16+
var __extends = (this && this.__extends) || (function () {
17+
var extendStatics = function (d, b) {
18+
extendStatics = Object.setPrototypeOf ||
19+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
20+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
21+
return extendStatics(d, b);
22+
};
23+
return function (d, b) {
24+
if (typeof b !== "function" && b !== null)
25+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
26+
extendStatics(d, b);
27+
function __() { this.constructor = d; }
28+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
29+
};
30+
})();
31+
var Assignment = /** @class */ (function (_super) {
32+
__extends(Assignment, _super);
33+
function Assignment() {
34+
var _this = _super.call(this) || this;
35+
_this.state = { version: 2 };
36+
return _this;
37+
}
38+
return Assignment;
39+
}(Base));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/inheritedConstructorPropertyContextualType.ts ===
2+
interface State {
3+
>State : Symbol(State, Decl(inheritedConstructorPropertyContextualType.ts, 0, 0))
4+
5+
version: 2
6+
>version : Symbol(State.version, Decl(inheritedConstructorPropertyContextualType.ts, 0, 17))
7+
}
8+
declare class Base<S> {
9+
>Base : Symbol(Base, Decl(inheritedConstructorPropertyContextualType.ts, 2, 1))
10+
>S : Symbol(S, Decl(inheritedConstructorPropertyContextualType.ts, 3, 19))
11+
12+
state: S
13+
>state : Symbol(Base.state, Decl(inheritedConstructorPropertyContextualType.ts, 3, 23))
14+
>S : Symbol(S, Decl(inheritedConstructorPropertyContextualType.ts, 3, 19))
15+
}
16+
class Assignment extends Base<State> {
17+
>Assignment : Symbol(Assignment, Decl(inheritedConstructorPropertyContextualType.ts, 5, 1))
18+
>Base : Symbol(Base, Decl(inheritedConstructorPropertyContextualType.ts, 2, 1))
19+
>State : Symbol(State, Decl(inheritedConstructorPropertyContextualType.ts, 0, 0))
20+
21+
constructor() {
22+
super()
23+
>super : Symbol(Base, Decl(inheritedConstructorPropertyContextualType.ts, 2, 1))
24+
25+
this.state = { version: 2 }
26+
>this.state : Symbol(Base.state, Decl(inheritedConstructorPropertyContextualType.ts, 3, 23))
27+
>this : Symbol(Assignment, Decl(inheritedConstructorPropertyContextualType.ts, 5, 1))
28+
>state : Symbol(Base.state, Decl(inheritedConstructorPropertyContextualType.ts, 3, 23))
29+
>version : Symbol(version, Decl(inheritedConstructorPropertyContextualType.ts, 9, 22))
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/inheritedConstructorPropertyContextualType.ts ===
2+
interface State {
3+
version: 2
4+
>version : 2
5+
}
6+
declare class Base<S> {
7+
>Base : Base<S>
8+
9+
state: S
10+
>state : S
11+
}
12+
class Assignment extends Base<State> {
13+
>Assignment : Assignment
14+
>Base : Base<State>
15+
16+
constructor() {
17+
super()
18+
>super() : void
19+
>super : typeof Base
20+
21+
this.state = { version: 2 }
22+
>this.state = { version: 2 } : { version: 2; }
23+
>this.state : State
24+
>this : this
25+
>state : State
26+
>{ version: 2 } : { version: 2; }
27+
>version : 2
28+
>2 : 2
29+
}
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
interface State {
2+
version: 2
3+
}
4+
declare class Base<S> {
5+
state: S
6+
}
7+
class Assignment extends Base<State> {
8+
constructor() {
9+
super()
10+
this.state = { version: 2 }
11+
}
12+
}

0 commit comments

Comments
 (0)