-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17455 from Microsoft/mappedTypeFixes
Mapped and indexed access type fixes
- Loading branch information
Showing
6 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
5 changes: 4 additions & 1 deletion
5
tests/baselines/reference/anyIndexedAccessArrayNoException.errors.txt
This file contains 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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
tests/cases/compiler/anyIndexedAccessArrayNoException.ts(1,12): error TS1122: A tuple type element list cannot be empty. | ||
tests/cases/compiler/anyIndexedAccessArrayNoException.ts(1,12): error TS2538: Type '[]' cannot be used as an index type. | ||
|
||
|
||
==== tests/cases/compiler/anyIndexedAccessArrayNoException.ts (1 errors) ==== | ||
==== tests/cases/compiler/anyIndexedAccessArrayNoException.ts (2 errors) ==== | ||
var x: any[[]]; | ||
~~ | ||
!!! error TS1122: A tuple type element list cannot be empty. | ||
~~ | ||
!!! error TS2538: Type '[]' cannot be used as an index type. | ||
|
This file contains 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
This file contains 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,35 @@ | ||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(9,30): error TS2536: Type 'K' cannot be used to index type 'T1<K>'. | ||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(13,30): error TS2536: Type 'K' cannot be used to index type 'T3'. | ||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2536: Type 'S' cannot be used to index type 'AB'. | ||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536: Type 'L' cannot be used to index type '{ [key in AB[S]]: true; }'. | ||
|
||
|
||
==== tests/cases/conformance/types/mapped/mappedTypeErrors2.ts (4 errors) ==== | ||
// Repros from #17238 | ||
|
||
type AB = { | ||
a: 'a' | ||
b: 'a' | ||
}; | ||
|
||
type T1<K extends keyof AB> = { [key in AB[K]]: true }; | ||
type T2<K extends 'a'|'b'> = T1<K>[K]; // Error | ||
~~~~~~~~ | ||
!!! error TS2536: Type 'K' cannot be used to index type 'T1<K>'. | ||
|
||
type R = AB[keyof AB]; // "a" | ||
type T3 = { [key in R]: true }; | ||
type T4<K extends 'a'|'b'> = T3[K] // Error | ||
~~~~~ | ||
!!! error TS2536: Type 'K' cannot be used to index type 'T3'. | ||
|
||
type T5<S extends 'a'|'b'|'extra'> = {[key in AB[S]]: true}[S]; // Error | ||
~~~~~ | ||
!!! error TS2536: Type 'S' cannot be used to index type 'AB'. | ||
|
||
type T6<S extends 'a'|'b', L extends 'a'|'b'> = {[key in AB[S]]: true}[L]; // Error | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2536: Type 'L' cannot be used to index type '{ [key in AB[S]]: true; }'. | ||
|
||
type T7<S extends 'a'|'b', L extends 'a'> = {[key in AB[S]]: true}[L]; | ||
|
This file contains 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,49 @@ | ||
//// [mappedTypeErrors2.ts] | ||
// Repros from #17238 | ||
|
||
type AB = { | ||
a: 'a' | ||
b: 'a' | ||
}; | ||
|
||
type T1<K extends keyof AB> = { [key in AB[K]]: true }; | ||
type T2<K extends 'a'|'b'> = T1<K>[K]; // Error | ||
|
||
type R = AB[keyof AB]; // "a" | ||
type T3 = { [key in R]: true }; | ||
type T4<K extends 'a'|'b'> = T3[K] // Error | ||
|
||
type T5<S extends 'a'|'b'|'extra'> = {[key in AB[S]]: true}[S]; // Error | ||
|
||
type T6<S extends 'a'|'b', L extends 'a'|'b'> = {[key in AB[S]]: true}[L]; // Error | ||
|
||
type T7<S extends 'a'|'b', L extends 'a'> = {[key in AB[S]]: true}[L]; | ||
|
||
|
||
//// [mappedTypeErrors2.js] | ||
// Repros from #17238 | ||
|
||
|
||
//// [mappedTypeErrors2.d.ts] | ||
declare type AB = { | ||
a: 'a'; | ||
b: 'a'; | ||
}; | ||
declare type T1<K extends keyof AB> = { | ||
[key in AB[K]]: true; | ||
}; | ||
declare type T2<K extends 'a' | 'b'> = T1<K>[K]; | ||
declare type R = AB[keyof AB]; | ||
declare type T3 = { | ||
[key in R]: true; | ||
}; | ||
declare type T4<K extends 'a' | 'b'> = T3[K]; | ||
declare type T5<S extends 'a' | 'b' | 'extra'> = { | ||
[key in AB[S]]: true; | ||
}[S]; | ||
declare type T6<S extends 'a' | 'b', L extends 'a' | 'b'> = { | ||
[key in AB[S]]: true; | ||
}[L]; | ||
declare type T7<S extends 'a' | 'b', L extends 'a'> = { | ||
[key in AB[S]]: true; | ||
}[L]; |
This file contains 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,22 @@ | ||
// @strictNullChecks: true | ||
// @declaration: true | ||
|
||
// Repros from #17238 | ||
|
||
type AB = { | ||
a: 'a' | ||
b: 'a' | ||
}; | ||
|
||
type T1<K extends keyof AB> = { [key in AB[K]]: true }; | ||
type T2<K extends 'a'|'b'> = T1<K>[K]; // Error | ||
|
||
type R = AB[keyof AB]; // "a" | ||
type T3 = { [key in R]: true }; | ||
type T4<K extends 'a'|'b'> = T3[K] // Error | ||
|
||
type T5<S extends 'a'|'b'|'extra'> = {[key in AB[S]]: true}[S]; // Error | ||
|
||
type T6<S extends 'a'|'b', L extends 'a'|'b'> = {[key in AB[S]]: true}[L]; // Error | ||
|
||
type T7<S extends 'a'|'b', L extends 'a'> = {[key in AB[S]]: true}[L]; |