You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Empty array's type is inferred as never[] even though array's type is known #20946
interfaceMyInterface{data?: [{x: number;}];}letmyVar: MyInterface={};/* ts-bug.ts(13,2): error TS2322: Type 'never[]' is not assignable to type '[{ x: number; }] | undefined'. Type 'never[]' is not assignable to type '[{ x: number; }]'. Property '0' is missing in type 'never[]'.*/myVar.data=[];
ts-bug.ts(13,1): error TS2322: Type 'never[]' is not assignable to type '[{ x: number; }] | undefined'.
Type 'never[]' is not assignable to type '[{ x: number; }]'.
Property '0' is missing in type 'never[]'.
Side notes:
The compilation works fine if data?: number[]; is specified.
I conjecture that the compilation breaks if and only if data's type is an inline object type:
data?: [{}] also breaks.
data?: MyObject[] with interface MyObject {} at the beginning of the file works!
data?: {}[], data?: Array<{x: number}>; and data?: {x: number}[]; work fine.
Related issues:
See also #9322, #13140. However, in these issues contrary to this one, the array's type has not been explicitly specified in the code.
Activity
[-]Empty array[/-][+]Empty array's type is inferred as never[] even though array's type is known[/+]ComFreek commentedon Dec 30, 2017
Never mind, I thought
[type](which is a tuple type) is the same astype[](an array).myVar.data = [{x: 42}]works as expected.