Skip to content

Commit

Permalink
Merge pull request #12652 from Microsoft/fixIndexedAccessWithAny
Browse files Browse the repository at this point in the history
Indexed access 'any[K]' is of type any
  • Loading branch information
ahejlsberg authored Dec 5, 2016
2 parents b7e8a6d + ee172cf commit 5c71de1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -589,6 +600,10 @@ var c1 = new Component1({
}
});
c1.get("hello");
function f(p) {
var a;
a[p].add; // any
}


//// [keyofAndIndexedAccess.d.ts]
Expand Down Expand Up @@ -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;
24 changes: 24 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5c71de1

Please sign in to comment.