Open e-davidson opened 5 years ago
Changed between 3.2 / 3.3
=== 3.4 ===
clipboard.ts:7 const k: T[K] = j.slice();
~
TS2322: Type 'U[]' is not assignable to type 'T[K]'.
=== 3.3 ===
clipboard.ts:7 const k: T[K] = j.slice();
~
TS2322: Type 'U[]' is not assignable to type 'T[K]'.
=== 3.2 ===
=== 3.1 ===
Another case of T[K]
type loss at slice()
call on generics array:
interface Table {
foo: string
bar: string | number
baz: number
}
type ArrayTable = { [K in keyof Table]: Table[K][] }
function get<K extends keyof Table>(key: K): Table[K] {
const arrayTable = {} as ArrayTable
const array = arrayTable[key] // MapTable[K]
const sliced = array.slice() // (string | number)[] , expected K[]
// Type '(string | number)[]' is not assignable to type 'Table[K]'.
// Type '(string | number)[]' is not assignable to type 'never'.
return sliced
}
TypeScript Version: 3.5.2 But this also reproduces in 3.1.6, maybe similar but different issue..?
TypeScript Version: 3.4.5
Search Terms: array property generics Code
While you can argue that U[] isn't necessarily assignable to type T[K]. Look at the following code. It's hard to see how TypeFromArray<T[K]>[]'s type should be any different than T[K]
Expected behavior:
No errors. T[K] and U[] should be assignable to each other. state[prop].slice(0) should be of the same type as state[prop] TypeFromArray<T[K]>[]'s type should be not be any different than T[K]
This code works in version 3.2.4 but stopped working in 3.3.1
Actual behavior:
Error Type 'U[]' is not assignable to type 'T[K]'. Error Type 'TypeFromArray<T[K]>[]' is not assignable to type 'T[K]'
Playground Link:
Playground Link
My main goal is to be able to pluck array properties and modify the arrays without losing the type T[K] The code above is simplified to show how TS is behaving. See my use case below.
It's hard to see how TypeFromArray<T[K]>[]'s type should be any different than T[K] The code below used to work when using 3.2.4