TypeScript is a non-type checking array constructed using Array () and filled with padding

TypeScript version 2.4.2 compiled with --target ES6

Line of code:

var coins: { coin: number}[] = [1,1,1]

calls TypeScript to quit

error TS2322: Type 'number []' is not assigned to type '{coin: number; } [] '

However, the line:

var coins: { coin: number}[] = Array(3).fill(1)

compiles successfully without errors.

Is this a TypeScript error, or is this the intended behavior (and not a check of the type of the array declared in this way)? If so, why?

+4
source share
1 answer

, Array(3) 3 any[], fill . any , any[] ( { coin: number})

Array, :

var coins: { coin: number}[] = Array<{ coin: number}>(3).fill(1) // 1 is not assignable to { coin: number}

, .

+4

Source: https://habr.com/ru/post/1693785/


All Articles