Skip to content

Empty array's type is inferred as never[] even though array's type is known #20946

Closed

Description

@ComFreek

TypeScript Version: 2.7.0-dev.20171230
Code

interface MyInterface {
	data?: [{
		x: number;
	}];
}

let myVar: 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 = [];

tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNEXT",
    "module": "commonjs",

    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true
  }
}

Expected behavior:

No compile error shall occur.

Actual behavior:

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

  1. changed the title [-]Empty array[/-] [+]Empty array's type is inferred as never[] even though array's type is known[/+] on Dec 30, 2017
  2. ComFreek commented on Dec 30, 2017

    @ComFreek
    Author

    Never mind, I thought [type] (which is a tuple type) is the same as type[] (an array). myVar.data = [{x: 42}] works as expected.

  3. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions