mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Exclude mapped types with 'as' clauses from certain checks (#47889)
* Exclude mapped types with 'as' clauses from certain checks * Add tests
This commit is contained in:
@@ -12220,6 +12220,7 @@ namespace ts {
|
||||
|
||||
function isMappedTypeGenericIndexedAccess(type: Type) {
|
||||
return type.flags & TypeFlags.IndexedAccess && getObjectFlags((type as IndexedAccessType).objectType) & ObjectFlags.Mapped &&
|
||||
!((type as IndexedAccessType).objectType as MappedType).declaration.nameType &&
|
||||
!isGenericMappedType((type as IndexedAccessType).objectType) && isGenericIndexType((type as IndexedAccessType).indexType);
|
||||
}
|
||||
|
||||
@@ -15656,7 +15657,7 @@ namespace ts {
|
||||
// If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper
|
||||
// that substitutes the index type for P. For example, for an index access { [P in K]: Box<T[P]> }[X], we
|
||||
// construct the type Box<T[X]>.
|
||||
if (isGenericMappedType(objectType)) {
|
||||
if (isGenericMappedType(objectType) && !objectType.declaration.nameType) {
|
||||
return type[cache] = mapType(substituteIndexedMappedType(objectType, type.indexType), t => getSimplifiedType(t, writing));
|
||||
}
|
||||
return type[cache] = type;
|
||||
@@ -26650,7 +26651,7 @@ namespace ts {
|
||||
|
||||
function getTypeOfPropertyOfContextualType(type: Type, name: __String, nameType?: Type) {
|
||||
return mapType(type, t => {
|
||||
if (isGenericMappedType(t)) {
|
||||
if (isGenericMappedType(t) && !t.declaration.nameType) {
|
||||
const constraint = getConstraintTypeFromMappedType(t);
|
||||
const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
|
||||
const propertyNameType = nameType || getStringLiteralType(unescapeLeadingUnderscores(name));
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(10,11): error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
|
||||
Type 'Mapped2<K>[`get${string}`]' is not assignable to type '{ a: K; }'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
|
||||
Type 'Mapped3<K>[string]' is not assignable to type '{ a: K; }'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
|
||||
'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts (3 errors) ====
|
||||
type Mapped1<K extends string> = { [P in K]: { a: P } };
|
||||
|
||||
function f1<K extends string>(obj: Mapped1<K>, key: K) {
|
||||
const x: { a: K } = obj[key];
|
||||
}
|
||||
|
||||
type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
|
||||
|
||||
function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
~
|
||||
!!! error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
|
||||
!!! error TS2322: Type 'Mapped2<K>[`get${string}`]' is not assignable to type '{ a: K; }'.
|
||||
}
|
||||
|
||||
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
|
||||
|
||||
function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
~
|
||||
!!! error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
|
||||
!!! error TS2322: Type 'Mapped3<K>[string]' is not assignable to type '{ a: K; }'.
|
||||
}
|
||||
|
||||
// Repro from #47794
|
||||
|
||||
type Foo<T extends string> = {
|
||||
[RemappedT in T as `get${RemappedT}`]: RemappedT;
|
||||
};
|
||||
|
||||
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
|
||||
~~~~~~~~~~~~~~
|
||||
!!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
|
||||
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
//// [mappedTypeConstraints2.ts]
|
||||
type Mapped1<K extends string> = { [P in K]: { a: P } };
|
||||
|
||||
function f1<K extends string>(obj: Mapped1<K>, key: K) {
|
||||
const x: { a: K } = obj[key];
|
||||
}
|
||||
|
||||
type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
|
||||
|
||||
function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
}
|
||||
|
||||
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
|
||||
|
||||
function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
}
|
||||
|
||||
// Repro from #47794
|
||||
|
||||
type Foo<T extends string> = {
|
||||
[RemappedT in T as `get${RemappedT}`]: RemappedT;
|
||||
};
|
||||
|
||||
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
|
||||
|
||||
|
||||
//// [mappedTypeConstraints2.js]
|
||||
"use strict";
|
||||
function f1(obj, key) {
|
||||
var x = obj[key];
|
||||
}
|
||||
function f2(obj, key) {
|
||||
var x = obj[key]; // Error
|
||||
}
|
||||
function f3(obj, key) {
|
||||
var x = obj[key]; // Error
|
||||
}
|
||||
var get = function (t, foo) { return foo["get".concat(t)]; }; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
|
||||
|
||||
|
||||
//// [mappedTypeConstraints2.d.ts]
|
||||
declare type Mapped1<K extends string> = {
|
||||
[P in K]: {
|
||||
a: P;
|
||||
};
|
||||
};
|
||||
declare function f1<K extends string>(obj: Mapped1<K>, key: K): void;
|
||||
declare type Mapped2<K extends string> = {
|
||||
[P in K as `get${P}`]: {
|
||||
a: P;
|
||||
};
|
||||
};
|
||||
declare function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`): void;
|
||||
declare type Mapped3<K extends string> = {
|
||||
[P in K as Uppercase<P>]: {
|
||||
a: P;
|
||||
};
|
||||
};
|
||||
declare function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>): void;
|
||||
declare type Foo<T extends string> = {
|
||||
[RemappedT in T as `get${RemappedT}`]: RemappedT;
|
||||
};
|
||||
declare const get: <T extends string>(t: T, foo: Foo<T>) => T;
|
||||
@@ -0,0 +1,106 @@
|
||||
=== tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts ===
|
||||
type Mapped1<K extends string> = { [P in K]: { a: P } };
|
||||
>Mapped1 : Symbol(Mapped1, Decl(mappedTypeConstraints2.ts, 0, 0))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 0, 13))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 0, 36))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 0, 13))
|
||||
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 0, 46))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 0, 36))
|
||||
|
||||
function f1<K extends string>(obj: Mapped1<K>, key: K) {
|
||||
>f1 : Symbol(f1, Decl(mappedTypeConstraints2.ts, 0, 56))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
|
||||
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 2, 30))
|
||||
>Mapped1 : Symbol(Mapped1, Decl(mappedTypeConstraints2.ts, 0, 0))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
|
||||
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 2, 46))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
|
||||
|
||||
const x: { a: K } = obj[key];
|
||||
>x : Symbol(x, Decl(mappedTypeConstraints2.ts, 3, 9))
|
||||
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 3, 14))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
|
||||
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 2, 30))
|
||||
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 2, 46))
|
||||
}
|
||||
|
||||
type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
|
||||
>Mapped2 : Symbol(Mapped2, Decl(mappedTypeConstraints2.ts, 4, 1))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 6, 13))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 6, 36))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 6, 13))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 6, 36))
|
||||
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 6, 59))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 6, 36))
|
||||
|
||||
function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
|
||||
>f2 : Symbol(f2, Decl(mappedTypeConstraints2.ts, 6, 69))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
|
||||
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 8, 30))
|
||||
>Mapped2 : Symbol(Mapped2, Decl(mappedTypeConstraints2.ts, 4, 1))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
|
||||
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 8, 46))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
|
||||
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
>x : Symbol(x, Decl(mappedTypeConstraints2.ts, 9, 9))
|
||||
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 9, 14))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
|
||||
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 8, 30))
|
||||
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 8, 46))
|
||||
}
|
||||
|
||||
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
|
||||
>Mapped3 : Symbol(Mapped3, Decl(mappedTypeConstraints2.ts, 10, 1))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 12, 13))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 12, 36))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 12, 13))
|
||||
>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 12, 36))
|
||||
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 12, 62))
|
||||
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 12, 36))
|
||||
|
||||
function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
|
||||
>f3 : Symbol(f3, Decl(mappedTypeConstraints2.ts, 12, 72))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
|
||||
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 14, 30))
|
||||
>Mapped3 : Symbol(Mapped3, Decl(mappedTypeConstraints2.ts, 10, 1))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
|
||||
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 14, 46))
|
||||
>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
|
||||
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
>x : Symbol(x, Decl(mappedTypeConstraints2.ts, 15, 9))
|
||||
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 15, 14))
|
||||
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
|
||||
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 14, 30))
|
||||
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 14, 46))
|
||||
}
|
||||
|
||||
// Repro from #47794
|
||||
|
||||
type Foo<T extends string> = {
|
||||
>Foo : Symbol(Foo, Decl(mappedTypeConstraints2.ts, 16, 1))
|
||||
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 20, 9))
|
||||
|
||||
[RemappedT in T as `get${RemappedT}`]: RemappedT;
|
||||
>RemappedT : Symbol(RemappedT, Decl(mappedTypeConstraints2.ts, 21, 5))
|
||||
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 20, 9))
|
||||
>RemappedT : Symbol(RemappedT, Decl(mappedTypeConstraints2.ts, 21, 5))
|
||||
>RemappedT : Symbol(RemappedT, Decl(mappedTypeConstraints2.ts, 21, 5))
|
||||
|
||||
};
|
||||
|
||||
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
|
||||
>get : Symbol(get, Decl(mappedTypeConstraints2.ts, 24, 5))
|
||||
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
|
||||
>t : Symbol(t, Decl(mappedTypeConstraints2.ts, 24, 31))
|
||||
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
|
||||
>foo : Symbol(foo, Decl(mappedTypeConstraints2.ts, 24, 36))
|
||||
>Foo : Symbol(Foo, Decl(mappedTypeConstraints2.ts, 16, 1))
|
||||
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
|
||||
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
|
||||
>foo : Symbol(foo, Decl(mappedTypeConstraints2.ts, 24, 36))
|
||||
>t : Symbol(t, Decl(mappedTypeConstraints2.ts, 24, 31))
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
=== tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts ===
|
||||
type Mapped1<K extends string> = { [P in K]: { a: P } };
|
||||
>Mapped1 : Mapped1<K>
|
||||
>a : P
|
||||
|
||||
function f1<K extends string>(obj: Mapped1<K>, key: K) {
|
||||
>f1 : <K extends string>(obj: Mapped1<K>, key: K) => void
|
||||
>obj : Mapped1<K>
|
||||
>key : K
|
||||
|
||||
const x: { a: K } = obj[key];
|
||||
>x : { a: K; }
|
||||
>a : K
|
||||
>obj[key] : Mapped1<K>[K]
|
||||
>obj : Mapped1<K>
|
||||
>key : K
|
||||
}
|
||||
|
||||
type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
|
||||
>Mapped2 : Mapped2<K>
|
||||
>a : P
|
||||
|
||||
function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
|
||||
>f2 : <K extends string>(obj: Mapped2<K>, key: `get${K}`) => void
|
||||
>obj : Mapped2<K>
|
||||
>key : `get${K}`
|
||||
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
>x : { a: K; }
|
||||
>a : K
|
||||
>obj[key] : Mapped2<K>[`get${K}`]
|
||||
>obj : Mapped2<K>
|
||||
>key : `get${K}`
|
||||
}
|
||||
|
||||
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
|
||||
>Mapped3 : Mapped3<K>
|
||||
>a : P
|
||||
|
||||
function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
|
||||
>f3 : <K extends string>(obj: Mapped3<K>, key: Uppercase<K>) => void
|
||||
>obj : Mapped3<K>
|
||||
>key : Uppercase<K>
|
||||
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
>x : { a: K; }
|
||||
>a : K
|
||||
>obj[key] : Mapped3<K>[Uppercase<K>]
|
||||
>obj : Mapped3<K>
|
||||
>key : Uppercase<K>
|
||||
}
|
||||
|
||||
// Repro from #47794
|
||||
|
||||
type Foo<T extends string> = {
|
||||
>Foo : Foo<T>
|
||||
|
||||
[RemappedT in T as `get${RemappedT}`]: RemappedT;
|
||||
};
|
||||
|
||||
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
|
||||
>get : <T extends string>(t: T, foo: Foo<T>) => T
|
||||
><T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`] : <T extends string>(t: T, foo: Foo<T>) => T
|
||||
>t : T
|
||||
>foo : Foo<T>
|
||||
>foo[`get${t}`] : Foo<T>[`get${T}`]
|
||||
>foo : Foo<T>
|
||||
>`get${t}` : `get${T}`
|
||||
>t : T
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
// @strict: true
|
||||
// @declaration: true
|
||||
|
||||
type Mapped1<K extends string> = { [P in K]: { a: P } };
|
||||
|
||||
function f1<K extends string>(obj: Mapped1<K>, key: K) {
|
||||
const x: { a: K } = obj[key];
|
||||
}
|
||||
|
||||
type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
|
||||
|
||||
function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
}
|
||||
|
||||
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
|
||||
|
||||
function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
|
||||
const x: { a: K } = obj[key]; // Error
|
||||
}
|
||||
|
||||
// Repro from #47794
|
||||
|
||||
type Foo<T extends string> = {
|
||||
[RemappedT in T as `get${RemappedT}`]: RemappedT;
|
||||
};
|
||||
|
||||
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
|
||||
Reference in New Issue
Block a user