Skip to content

Commit cf8798d

Browse files
authored
Make origin union cache key unique for key lists still under construction (#43339)
1 parent d1b4342 commit cf8798d

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13823,7 +13823,7 @@ namespace ts {
1382313823
const typeKey = !origin ? getTypeListId(types) :
1382413824
origin.flags & TypeFlags.Union ? `|${getTypeListId((<UnionType>origin).types)}` :
1382513825
origin.flags & TypeFlags.Intersection ? `&${getTypeListId((<IntersectionType>origin).types)}` :
13826-
`#${(<IndexType>origin).type.id}`;
13826+
`#${(<IndexType>origin).type.id}|${getTypeListId(types)}`; // origin type id alone is insufficient, as `keyof x` may resolve to multiple WIP values while `x` is still resolving
1382713827
const id = typeKey + getAliasId(aliasSymbol, aliasTypeArguments);
1382813828
let type = unionTypes.get(id);
1382913829
if (!type) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//// [keyofGenericExtendingClassDoubleLayer.ts]
2+
class Model<Attributes = any> {
3+
public createdAt: Date;
4+
}
5+
6+
type ModelAttributes<T> = Omit<T, keyof Model>;
7+
8+
class AutoModel<T> extends Model<ModelAttributes<T>> {}
9+
10+
class PersonModel extends AutoModel<PersonModel> {
11+
public age: number;
12+
13+
toJson() {
14+
let x: keyof this = 'createdAt';
15+
}
16+
}
17+
18+
19+
//// [keyofGenericExtendingClassDoubleLayer.js]
20+
var __extends = (this && this.__extends) || (function () {
21+
var extendStatics = function (d, b) {
22+
extendStatics = Object.setPrototypeOf ||
23+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
24+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
25+
return extendStatics(d, b);
26+
};
27+
return function (d, b) {
28+
if (typeof b !== "function" && b !== null)
29+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
30+
extendStatics(d, b);
31+
function __() { this.constructor = d; }
32+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
33+
};
34+
})();
35+
var Model = /** @class */ (function () {
36+
function Model() {
37+
}
38+
return Model;
39+
}());
40+
var AutoModel = /** @class */ (function (_super) {
41+
__extends(AutoModel, _super);
42+
function AutoModel() {
43+
return _super !== null && _super.apply(this, arguments) || this;
44+
}
45+
return AutoModel;
46+
}(Model));
47+
var PersonModel = /** @class */ (function (_super) {
48+
__extends(PersonModel, _super);
49+
function PersonModel() {
50+
return _super !== null && _super.apply(this, arguments) || this;
51+
}
52+
PersonModel.prototype.toJson = function () {
53+
var x = 'createdAt';
54+
};
55+
return PersonModel;
56+
}(AutoModel));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/compiler/keyofGenericExtendingClassDoubleLayer.ts ===
2+
class Model<Attributes = any> {
3+
>Model : Symbol(Model, Decl(keyofGenericExtendingClassDoubleLayer.ts, 0, 0))
4+
>Attributes : Symbol(Attributes, Decl(keyofGenericExtendingClassDoubleLayer.ts, 0, 12))
5+
6+
public createdAt: Date;
7+
>createdAt : Symbol(Model.createdAt, Decl(keyofGenericExtendingClassDoubleLayer.ts, 0, 31))
8+
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
9+
}
10+
11+
type ModelAttributes<T> = Omit<T, keyof Model>;
12+
>ModelAttributes : Symbol(ModelAttributes, Decl(keyofGenericExtendingClassDoubleLayer.ts, 2, 1))
13+
>T : Symbol(T, Decl(keyofGenericExtendingClassDoubleLayer.ts, 4, 21))
14+
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
15+
>T : Symbol(T, Decl(keyofGenericExtendingClassDoubleLayer.ts, 4, 21))
16+
>Model : Symbol(Model, Decl(keyofGenericExtendingClassDoubleLayer.ts, 0, 0))
17+
18+
class AutoModel<T> extends Model<ModelAttributes<T>> {}
19+
>AutoModel : Symbol(AutoModel, Decl(keyofGenericExtendingClassDoubleLayer.ts, 4, 47))
20+
>T : Symbol(T, Decl(keyofGenericExtendingClassDoubleLayer.ts, 6, 16))
21+
>Model : Symbol(Model, Decl(keyofGenericExtendingClassDoubleLayer.ts, 0, 0))
22+
>ModelAttributes : Symbol(ModelAttributes, Decl(keyofGenericExtendingClassDoubleLayer.ts, 2, 1))
23+
>T : Symbol(T, Decl(keyofGenericExtendingClassDoubleLayer.ts, 6, 16))
24+
25+
class PersonModel extends AutoModel<PersonModel> {
26+
>PersonModel : Symbol(PersonModel, Decl(keyofGenericExtendingClassDoubleLayer.ts, 6, 55))
27+
>AutoModel : Symbol(AutoModel, Decl(keyofGenericExtendingClassDoubleLayer.ts, 4, 47))
28+
>PersonModel : Symbol(PersonModel, Decl(keyofGenericExtendingClassDoubleLayer.ts, 6, 55))
29+
30+
public age: number;
31+
>age : Symbol(PersonModel.age, Decl(keyofGenericExtendingClassDoubleLayer.ts, 8, 50))
32+
33+
toJson() {
34+
>toJson : Symbol(PersonModel.toJson, Decl(keyofGenericExtendingClassDoubleLayer.ts, 9, 23))
35+
36+
let x: keyof this = 'createdAt';
37+
>x : Symbol(x, Decl(keyofGenericExtendingClassDoubleLayer.ts, 12, 11))
38+
}
39+
}
40+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/keyofGenericExtendingClassDoubleLayer.ts ===
2+
class Model<Attributes = any> {
3+
>Model : Model<Attributes>
4+
5+
public createdAt: Date;
6+
>createdAt : Date
7+
}
8+
9+
type ModelAttributes<T> = Omit<T, keyof Model>;
10+
>ModelAttributes : ModelAttributes<T>
11+
12+
class AutoModel<T> extends Model<ModelAttributes<T>> {}
13+
>AutoModel : AutoModel<T>
14+
>Model : Model<ModelAttributes<T>>
15+
16+
class PersonModel extends AutoModel<PersonModel> {
17+
>PersonModel : PersonModel
18+
>AutoModel : AutoModel<PersonModel>
19+
20+
public age: number;
21+
>age : number
22+
23+
toJson() {
24+
>toJson : () => void
25+
26+
let x: keyof this = 'createdAt';
27+
>x : keyof this
28+
>'createdAt' : "createdAt"
29+
}
30+
}
31+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Model<Attributes = any> {
2+
public createdAt: Date;
3+
}
4+
5+
type ModelAttributes<T> = Omit<T, keyof Model>;
6+
7+
class AutoModel<T> extends Model<ModelAttributes<T>> {}
8+
9+
class PersonModel extends AutoModel<PersonModel> {
10+
public age: number;
11+
12+
toJson() {
13+
let x: keyof this = 'createdAt';
14+
}
15+
}

0 commit comments

Comments
 (0)