Skip to content

Fix indexed access relation #21242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9632,7 +9632,7 @@ namespace ts {
}
else if (target.flags & TypeFlags.IndexedAccess) {
// A type S is related to a type T[K] if S is related to A[K], where K is string-like and
// A is the apparent type of T.
// A is the constraint of T.
const constraint = getConstraintOfIndexedAccess(<IndexedAccessType>target);
if (constraint) {
if (result = isRelatedTo(source, constraint, reportErrors)) {
Expand Down Expand Up @@ -9668,18 +9668,19 @@ namespace ts {
}
else if (source.flags & TypeFlags.IndexedAccess) {
// A type S[K] is related to a type T if A[K] is related to T, where K is string-like and
// A is the apparent type of S.
// A is the constraint of S.
const constraint = getConstraintOfIndexedAccess(<IndexedAccessType>source);
if (constraint) {
if (result = isRelatedTo(constraint, target, reportErrors)) {
errorInfo = saveErrorInfo;
return result;
}
}
else if (target.flags & TypeFlags.IndexedAccess && (<IndexedAccessType>source).indexType === (<IndexedAccessType>target).indexType) {
// if we have indexed access types with identical index types, see if relationship holds for
// the two object types.
else if (target.flags & TypeFlags.IndexedAccess) {
if (result = isRelatedTo((<IndexedAccessType>source).objectType, (<IndexedAccessType>target).objectType, reportErrors)) {
result &= isRelatedTo((<IndexedAccessType>source).indexType, (<IndexedAccessType>target).indexType, reportErrors);
}
if (result) {
errorInfo = saveErrorInfo;
return result;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,19 @@ class AnotherSampleClass<T> extends SampleClass<T & Foo> {
}
}
new AnotherSampleClass({});

// Positive repro from #17166
function f3<T, K extends keyof T>(t: T, k: K, tk: T[K]): void {
for (let key in t) {
key = k // ok, K ==> keyof T
t[key] = tk; // ok, T[K] ==> T[keyof T]
}
}

// # 21185
type Predicates<TaggedRecord> = {
[T in keyof TaggedRecord]: (variant: TaggedRecord[keyof TaggedRecord]) => variant is TaggedRecord[T]
}


//// [keyofAndIndexedAccess.js]
Expand Down Expand Up @@ -928,6 +941,13 @@ var AnotherSampleClass = /** @class */ (function (_super) {
return AnotherSampleClass;
}(SampleClass));
new AnotherSampleClass({});
// Positive repro from #17166
function f3(t, k, tk) {
for (var key in t) {
key = k; // ok, K ==> keyof T
t[key] = tk; // ok, T[K] ==> T[keyof T]
}
}


//// [keyofAndIndexedAccess.d.ts]
Expand Down Expand Up @@ -1188,3 +1208,7 @@ declare class AnotherSampleClass<T> extends SampleClass<T & Foo> {
constructor(props: T);
brokenMethod(): void;
}
declare function f3<T, K extends keyof T>(t: T, k: K, tk: T[K]): void;
declare type Predicates<TaggedRecord> = {
[T in keyof TaggedRecord]: (variant: TaggedRecord[keyof TaggedRecord]) => variant is TaggedRecord[T];
};
45 changes: 45 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -1962,3 +1962,48 @@ class AnotherSampleClass<T> extends SampleClass<T & Foo> {
new AnotherSampleClass({});
>AnotherSampleClass : Symbol(AnotherSampleClass, Decl(keyofAndIndexedAccess.ts, 540, 54))

// Positive repro from #17166
function f3<T, K extends keyof T>(t: T, k: K, tk: T[K]): void {
>f3 : Symbol(f3, Decl(keyofAndIndexedAccess.ts, 552, 27))
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 555, 12))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 555, 14))
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 555, 12))
>t : Symbol(t, Decl(keyofAndIndexedAccess.ts, 555, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 555, 12))
>k : Symbol(k, Decl(keyofAndIndexedAccess.ts, 555, 39))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 555, 14))
>tk : Symbol(tk, Decl(keyofAndIndexedAccess.ts, 555, 45))
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 555, 12))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 555, 14))

for (let key in t) {
>key : Symbol(key, Decl(keyofAndIndexedAccess.ts, 556, 12))
>t : Symbol(t, Decl(keyofAndIndexedAccess.ts, 555, 34))

key = k // ok, K ==> keyof T
>key : Symbol(key, Decl(keyofAndIndexedAccess.ts, 556, 12))
>k : Symbol(k, Decl(keyofAndIndexedAccess.ts, 555, 39))

t[key] = tk; // ok, T[K] ==> T[keyof T]
>t : Symbol(t, Decl(keyofAndIndexedAccess.ts, 555, 34))
>key : Symbol(key, Decl(keyofAndIndexedAccess.ts, 556, 12))
>tk : Symbol(tk, Decl(keyofAndIndexedAccess.ts, 555, 45))
}
}

// # 21185
type Predicates<TaggedRecord> = {
>Predicates : Symbol(Predicates, Decl(keyofAndIndexedAccess.ts, 560, 1))
>TaggedRecord : Symbol(TaggedRecord, Decl(keyofAndIndexedAccess.ts, 563, 16))

[T in keyof TaggedRecord]: (variant: TaggedRecord[keyof TaggedRecord]) => variant is TaggedRecord[T]
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 564, 3))
>TaggedRecord : Symbol(TaggedRecord, Decl(keyofAndIndexedAccess.ts, 563, 16))
>variant : Symbol(variant, Decl(keyofAndIndexedAccess.ts, 564, 30))
>TaggedRecord : Symbol(TaggedRecord, Decl(keyofAndIndexedAccess.ts, 563, 16))
>TaggedRecord : Symbol(TaggedRecord, Decl(keyofAndIndexedAccess.ts, 563, 16))
>variant : Symbol(variant, Decl(keyofAndIndexedAccess.ts, 564, 30))
>TaggedRecord : Symbol(TaggedRecord, Decl(keyofAndIndexedAccess.ts, 563, 16))
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 564, 3))
}

48 changes: 48 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.types
Original file line number Diff line number Diff line change
Expand Up @@ -2294,3 +2294,51 @@ new AnotherSampleClass({});
>AnotherSampleClass : typeof AnotherSampleClass
>{} : {}

// Positive repro from #17166
function f3<T, K extends keyof T>(t: T, k: K, tk: T[K]): void {
>f3 : <T, K extends keyof T>(t: T, k: K, tk: T[K]) => void
>T : T
>K : K
>T : T
>t : T
>T : T
>k : K
>K : K
>tk : T[K]
>T : T
>K : K

for (let key in t) {
>key : keyof T
>t : T

key = k // ok, K ==> keyof T
>key = k : K
>key : keyof T
>k : K

t[key] = tk; // ok, T[K] ==> T[keyof T]
>t[key] = tk : T[K]
>t[key] : T[keyof T]
>t : T
>key : keyof T
>tk : T[K]
}
}

// # 21185
type Predicates<TaggedRecord> = {
>Predicates : Predicates<TaggedRecord>
>TaggedRecord : TaggedRecord

[T in keyof TaggedRecord]: (variant: TaggedRecord[keyof TaggedRecord]) => variant is TaggedRecord[T]
>T : T
>TaggedRecord : TaggedRecord
>variant : TaggedRecord[keyof TaggedRecord]
>TaggedRecord : TaggedRecord
>TaggedRecord : TaggedRecord
>variant : any
>TaggedRecord : TaggedRecord
>T : T
}

53 changes: 45 additions & 8 deletions tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(76,5): error
Type 'T' is not assignable to type 'T & U'.
Type 'T' is not assignable to type 'U'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(77,5): error TS2322: Type 'keyof (T & U)' is not assignable to type 'keyof (T | U)'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(84,9): error TS2322: Type 'keyof T' is not assignable to type 'K'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(85,9): error TS2322: Type 'T[keyof T]' is not assignable to type 'T[K]'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(86,9): error TS2322: Type 'keyof T' is not assignable to type 'K'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(88,9): error TS2322: Type 'T[keyof T]' is not assignable to type 'T[K]'.
Type 'keyof T' is not assignable to type 'K'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(91,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K]'.
Type 'T' is not assignable to type 'U'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(94,5): error TS2322: Type 'T[J]' is not assignable to type 'U[J]'.
Type 'T' is not assignable to type 'U'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(97,5): error TS2322: Type 'T[K]' is not assignable to type 'T[J]'.
Type 'K' is not assignable to type 'J'.
Type 'keyof T' is not assignable to type 'J'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(100,5): error TS2322: Type 'T[K]' is not assignable to type 'U[J]'.
Type 'T' is not assignable to type 'U'.


==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (27 errors) ====
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (31 errors) ====
class Shape {
name: string;
width: number;
Expand Down Expand Up @@ -167,15 +177,42 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(85,9): error
}

// Repro from #17166
function f3<T, K extends keyof T>(obj: T, k: K, value: T[K]): void {
for (let key in obj) {
function f3<T, K extends keyof T, U extends T, J extends K>(
t: T, k: K, tk: T[K], u: U, j: J, uk: U[K], tj: T[J], uj: U[J]): void {
for (let key in t) {
key = k // ok, K ==> keyof T
k = key // error, keyof T =/=> K
~
!!! error TS2322: Type 'keyof T' is not assignable to type 'K'.
value = obj[key]; // error, T[keyof T] =/=> T[K]
~~~~~
t[key] = tk; // ok, T[K] ==> T[keyof T]
tk = t[key]; // error, T[keyof T] =/=> T[K]
~~
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'T[K]'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'K'.
}
}
tk = uk;
uk = tk; // error
~~
!!! error TS2322: Type 'T[K]' is not assignable to type 'U[K]'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.

tj = uj;
uj = tj; // error
~~
!!! error TS2322: Type 'T[J]' is not assignable to type 'U[J]'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.

tk = tj;
tj = tk; // error
~~
!!! error TS2322: Type 'T[K]' is not assignable to type 'T[J]'.
!!! error TS2322: Type 'K' is not assignable to type 'J'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'J'.

tk = uj;
uj = tk; // error
~~
!!! error TS2322: Type 'T[K]' is not assignable to type 'U[J]'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.
}

37 changes: 30 additions & 7 deletions tests/baselines/reference/keyofAndIndexedAccessErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,26 @@ function f20<T, U>(k1: keyof (T | U), k2: keyof (T & U), o1: T | U, o2: T & U) {
}

// Repro from #17166
function f3<T, K extends keyof T>(obj: T, k: K, value: T[K]): void {
for (let key in obj) {
function f3<T, K extends keyof T, U extends T, J extends K>(
t: T, k: K, tk: T[K], u: U, j: J, uk: U[K], tj: T[J], uj: U[J]): void {
for (let key in t) {
key = k // ok, K ==> keyof T
k = key // error, keyof T =/=> K
value = obj[key]; // error, T[keyof T] =/=> T[K]
t[key] = tk; // ok, T[K] ==> T[keyof T]
tk = t[key]; // error, T[keyof T] =/=> T[K]
}
}
tk = uk;
uk = tk; // error

tj = uj;
uj = tj; // error

tk = tj;
tj = tk; // error

tk = uj;
uj = tk; // error
}


//// [keyofAndIndexedAccessErrors.js]
Expand Down Expand Up @@ -120,9 +133,19 @@ function f20(k1, k2, o1, o2) {
k2 = k1;
}
// Repro from #17166
function f3(obj, k, value) {
for (var key in obj) {
function f3(t, k, tk, u, j, uk, tj, uj) {
for (var key in t) {
key = k; // ok, K ==> keyof T
k = key; // error, keyof T =/=> K
value = obj[key]; // error, T[keyof T] =/=> T[K]
t[key] = tk; // ok, T[K] ==> T[keyof T]
tk = t[key]; // error, T[keyof T] =/=> T[K]
}
tk = uk;
uk = tk; // error
tj = uj;
uj = tj; // error
tk = tj;
tj = tk; // error
tk = uj;
uj = tk; // error
}
Loading