Skip to content

Union type of a interface with array of the same interface fails to check interface optional properties #8768

Closed
@matthauck

Description

@matthauck

TypeScript Version:

1.8.10

Code

interface SomeInterface {
    name?: string;
}

// these two (correctly) produce compiler errors
//var broken1: SomeInterface = { another: 'a' };
//var broken2: SomeInterface[] = { another: 'b' };

// this does not produce a compiler error but should
var broken3: SomeInterface | SomeInterface[] = { another: 'c' };

Expected behavior:
Compilation should fail

Actual behavior:
Compilation succeeds

Note: This behavior only seems to manifest when the interface contains only optional properties. If I change name to be a required property then compilation fails with:

test.ts(10,5): error TS2322: Type '{ [x: number]: undefined; another: string; }' is not assignable to type 'SomeInterface | SomeInterface[]'.
  Type '{ [x: number]: undefined; another: string; }' is not assignable to type 'SomeInterface[]'.
    Property 'length' is missing in type '{ [x: number]: undefined; another: string; }'.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions