Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matthauck opened this issue May 23, 2016 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@matthauck
Copy link

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; }'.
@RyanCavanaugh
Copy link
Member

@ahejlsberg seems like this should have been fixed when we fixed #5237 ?

@mhegazy mhegazy added the Bug A bug in TypeScript label May 23, 2016
@ahejlsberg
Copy link
Member

We currently do the excess property check only if no constituent of the target has an index signature (of either kind). An array has only a numeric index signature and we probably should factor that into our reasoning. Specifically, we should say that if some constituent of the target has a numeric index signature, then only numerically named properties are expected.

@sandersn
Copy link
Member

The fix is up at #8822.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 26, 2016
@matthauck
Copy link
Author

Thanks! 🎉

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants