不知道如何如何fomulate的問題,但這樣的話:TS:爲什麼可以將無效類型分配給泛型類型變量?
interface X {
some: number
}
let arr1: Array<X> = Array.from([{ some: 1, another: 2 }]) // no error
let arr2: Array<X> = Array.from<X>([{ some: 1, another: 2 }]) // will error
錯誤:
Argument of type '{ some: number; another: number; }[]' is not assignable to parameter of type 'ArrayLike<X>'.
Index signatures are incompatible.
Type '{ some: number; another: number; }' is not assignable to type 'X'.
Object literal may only specify known properties, and 'another' does not exist in type 'X'.
爲什麼在第一種情況下沒有任何錯誤(沒有類型的可比性檢查),是由設計還是有這個問題?
此限制不限於數組,並在[本答案](http://stackoverflow.com/a/31816062/43848)中有詳細解釋, – artem