Merge pull request #12652 from Microsoft/fixIndexedAccessWithAny

Indexed access 'any[K]' is of type any
This commit is contained in:
Anders Hejlsberg 2016-12-05 06:30:12 -08:00 committed by GitHub
commit 5c71de103a
5 changed files with 84 additions and 0 deletions

View file

@ -6033,6 +6033,9 @@ namespace ts {
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode) {
if (maybeTypeOfKind(indexType, TypeFlags.TypeVariable | TypeFlags.Index) || isGenericMappedType(objectType)) {
if (objectType.flags & TypeFlags.Any) {
return objectType;
}
// If the index type is generic or if the object type is a mapped type with a generic constraint,
// we are performing a higher-order index access where we cannot meaningfully access the properties
// of the object type. In those cases, we first check that the index type is assignable to 'keyof T'

View file

@ -353,6 +353,17 @@ interface Options2<Data, Computed> {
declare class Component2<Data, Computed> {
constructor(options: Options2<Data, Computed>);
get<K extends keyof Data | keyof Computed>(key: K): (Data & Computed)[K];
}
// Repro from #12641
interface R {
p: number;
}
function f<K extends keyof R>(p: K) {
let a: any;
a[p].add; // any
}
//// [keyofAndIndexedAccess.js]
@ -589,6 +600,10 @@ var c1 = new Component1({
}
});
c1.get("hello");
function f(p) {
var a;
a[p].add; // any
}
//// [keyofAndIndexedAccess.d.ts]
@ -757,3 +772,7 @@ declare class Component2<Data, Computed> {
constructor(options: Options2<Data, Computed>);
get<K extends keyof Data | keyof Computed>(key: K): (Data & Computed)[K];
}
interface R {
p: number;
}
declare function f<K extends keyof R>(p: K): void;

View file

@ -1320,3 +1320,27 @@ declare class Component2<Data, Computed> {
>Computed : Symbol(Computed, Decl(keyofAndIndexedAccess.ts, 351, 30))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 353, 8))
}
// Repro from #12641
interface R {
>R : Symbol(R, Decl(keyofAndIndexedAccess.ts, 354, 1))
p: number;
>p : Symbol(R.p, Decl(keyofAndIndexedAccess.ts, 358, 13))
}
function f<K extends keyof R>(p: K) {
>f : Symbol(f, Decl(keyofAndIndexedAccess.ts, 360, 1))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 362, 11))
>R : Symbol(R, Decl(keyofAndIndexedAccess.ts, 354, 1))
>p : Symbol(p, Decl(keyofAndIndexedAccess.ts, 362, 30))
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 362, 11))
let a: any;
>a : Symbol(a, Decl(keyofAndIndexedAccess.ts, 363, 7))
a[p].add; // any
>a : Symbol(a, Decl(keyofAndIndexedAccess.ts, 363, 7))
>p : Symbol(p, Decl(keyofAndIndexedAccess.ts, 362, 30))
}

View file

@ -1560,3 +1560,30 @@ declare class Component2<Data, Computed> {
>Computed : Computed
>K : K
}
// Repro from #12641
interface R {
>R : R
p: number;
>p : number
}
function f<K extends keyof R>(p: K) {
>f : <K extends "p">(p: K) => void
>K : K
>R : R
>p : K
>K : K
let a: any;
>a : any
a[p].add; // any
>a[p].add : any
>a[p] : any
>a : any
>p : K
>add : any
}

View file

@ -354,4 +354,15 @@ interface Options2<Data, Computed> {
declare class Component2<Data, Computed> {
constructor(options: Options2<Data, Computed>);
get<K extends keyof Data | keyof Computed>(key: K): (Data & Computed)[K];
}
// Repro from #12641
interface R {
p: number;
}
function f<K extends keyof R>(p: K) {
let a: any;
a[p].add; // any
}