Skip to content

allow an object narrowed to a certain interface be used where such interface is expected #12919

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
zpdDG4gta8XKpMCd opened this issue Dec 14, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Dec 14, 2016

interface A { value: number | string };
interface B { value: number; }
function takeOnlyB(value: B): void {}
const data: A = { value: 'hey' };
data.value = 1; // <-- from this point on we know for sure that value is number
takeOnlyB(data);  // <-- problem, despite effectively being of type `B` data still can't be used

// expected:  takeOnlyB should accept data because it's been asserted to comply with `B`
Argument of type 'A' is not assignable to parameter of type 'B'.
  Types of property 'value' are incompatible.
    Type 'string | number' is not assignable to type 'number'.
      Type 'string' is not assignable to type 'number'.
@zpdDG4gta8XKpMCd zpdDG4gta8XKpMCd changed the title allow an object narrowed a certain interface be used where such interface is expected allow an object narrowed to a certain interface be used where such interface is expected Dec 14, 2016
@RyanCavanaugh
Copy link
Member

Duplicate #10065

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 14, 2016
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants