Merge pull request #23966 from Microsoft/fixIndexedAccessAnyConstraint

Fix indexed access with 'any' constraint
This commit is contained in:
Anders Hejlsberg 2018-05-08 12:21:00 -07:00 committed by GitHub
commit 40e0ab72d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 1 deletions

View File

@ -10755,7 +10755,7 @@ namespace ts {
}
}
const constraint = getConstraintForRelation(<TypeParameter>source);
if (!constraint || constraint.flags & TypeFlags.Any) {
if (!constraint || (source.flags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) {
// A type variable with no constraint is not related to the non-primitive object type.
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
errorInfo = saveErrorInfo;

View File

@ -627,6 +627,14 @@ class Unbounded<T> {
let y: {} | undefined | null = x;
}
}
// Repro from #23940
interface I7 {
x: any;
}
type Foo7<T extends number> = T;
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;
//// [keyofAndIndexedAccess.js]
@ -1368,3 +1376,8 @@ declare function fn<T extends I, K extends keyof T>(o: T, k: K): void;
declare class Unbounded<T> {
foo(x: T[keyof T]): void;
}
interface I7 {
x: any;
}
declare type Foo7<T extends number> = T;
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;

View File

@ -2235,3 +2235,26 @@ class Unbounded<T> {
}
}
// Repro from #23940
interface I7 {
>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1))
x: any;
>x : Symbol(I7.x, Decl(keyofAndIndexedAccess.ts, 631, 14))
}
type Foo7<T extends number> = T;
>Foo7 : Symbol(Foo7, Decl(keyofAndIndexedAccess.ts, 633, 1))
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 634, 10))
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 634, 10))
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;
>f7 : Symbol(f7, Decl(keyofAndIndexedAccess.ts, 634, 32))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20))
>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1))
>type : Symbol(type, Decl(keyofAndIndexedAccess.ts, 635, 40))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20))
>Foo7 : Symbol(Foo7, Decl(keyofAndIndexedAccess.ts, 633, 1))
>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20))

View File

@ -2591,3 +2591,26 @@ class Unbounded<T> {
}
}
// Repro from #23940
interface I7 {
>I7 : I7
x: any;
>x : any
}
type Foo7<T extends number> = T;
>Foo7 : T
>T : T
>T : T
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;
>f7 : <K extends "x">(type: K) => I7[K]
>K : K
>I7 : I7
>type : K
>K : K
>Foo7 : T
>I7 : I7
>K : K

View File

@ -629,3 +629,11 @@ class Unbounded<T> {
let y: {} | undefined | null = x;
}
}
// Repro from #23940
interface I7 {
x: any;
}
type Foo7<T extends number> = T;
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;