- Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixedFixedA PR has been merged for this issueA PR has been merged for this issue
Description
TypeScript Version: 2.0.0 beta
Functions typed to accept union typed params incorrectly typecheck against functions whose params only match a subset of the union. Example:
interfaceCallback{(value: string|number): void;}// expect this to workconstmany: Callback=(value: string|number|boolean)=>{// ...}// expect a type error hereconstone: Callback=(value: string)=>{// ...}many(1);// should work, does workone(1);// shouldn't work, does work *many("hello");// should work, does workone("hello");// should work, does workmany(true);// shouldn't work, doesn't workone(true);// shouldn't work, doesn't workExpectedone throws a type error, since string | number is not assignable to string.
Actualone compiles fine.
This comes up in real code when declaring a callback whose params may not be defined, e.g.
interfaceSomeLib{onChange: (callback: (newValue: string|null)=>void)=>void;}constfoo: SomeLib= ...;foo.onChange((newValue: string)=>{// oops, I forgot newValue may not be defined, but this compiles});Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixedFixedA PR has been merged for this issueA PR has been merged for this issue