Merge pull request #12652 from Microsoft/fixIndexedAccessWithAny
Indexed access 'any[K]' is of type any
This commit is contained in:
commit
5c71de103a
|
@ -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'
|
||||
|
|
|
@ -355,6 +355,17 @@ declare class Component2<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]
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
|
@ -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;
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -355,3 +355,14 @@ 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
|
||||
}
|
Loading…
Reference in a new issue