-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Exclude mapped types with 'as' clauses from certain checks #47889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/mappedTypeConstraints2.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`]'. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
106 changes: 106 additions & 0 deletions
106
tests/baselines/reference/mappedTypeConstraints2.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
28 changes: 28 additions & 0 deletions
28
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we get this error elaboration here? I find it confusing to state that
'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I never find that error elaboration useful, and we include it every time an assignment to a type parameter fails. I think we should get rid of it, but that would be another PR.