Closed
Description
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; }'.