Skip to content

Error using intersection type for object rest destructuring #12520

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
billti opened this issue Nov 27, 2016 · 3 comments
Closed

Error using intersection type for object rest destructuring #12520

billti opened this issue Nov 27, 2016 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@billti
Copy link
Member

billti commented Nov 27, 2016

This is using the latest release-2.1 branch. I'm unclear why types B & C would behave differently below when it comes to object rest members, so I am assuming this is a bug. (ping @sandersn & @mhegazy ).

interface A {
    name: string;
    age: number;
}

interface B extends A {
    address: string;
}

type C = A & {address: string};

// Both v1 & v2 appear to have the same members...
var v1: B;
var v2: C;

// ... and assignment in either direction is fine
v1 = v2; // OK
v2 = v1; // OK

// let {name, ...x2} = v1;  // This line is valid (when uncommented)
let {name, ...x3} = v2;  // This line gives the error: Rest types may only be created from object types.
@sandersn
Copy link
Member

sandersn commented Nov 27, 2016 via email

@sandersn sandersn added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 27, 2016
@billti
Copy link
Member Author

billti commented Nov 27, 2016

My scenario was in trying to type a tree of React components, whereby (parts of) the model gets passed down, yet some components may also add members for some children (such as event handlers). A simplified/contrived example is below. Without intersection types working, I had to extend an interface each time I wanted to augment the type of the receiving props parameter.

let model = {
    a: "test",
    b: 42,
    c: true
};
type Model = typeof model;

function Comp1(model: Model){
    let clickHandler = (e: Event) => {return true;};
    return <Comp2 onClick={clickHandler} {...model}></Comp2>;
}

function Comp2({...props}: Model & {onClick: Function}){ // <- Gives an error currently
    return <div></div>;
}

Note: Subtraction types will be nice too, as often you remove a couple of props from the model then pass down the remainder - which is kind of a pain to describe accurately right now without a lot of forethought (and code).

@mhegazy mhegazy added Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Nov 28, 2016
@mhegazy mhegazy added this to the TypeScript 2.1.3 milestone Nov 28, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Nov 28, 2016

In cases like these we should just allow it to go through. it is rather artificial that we do not. type parameters should still be disallowed though.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 30, 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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants