-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
That's by design since (1) I don't know of a scenario that depends on it (2) I expect in 2.2 we'll have real rest and spread types that handle intersections correctly.
Currently intersections of object types are not object types themselves. They stay intersections, which makes it hard to track the difference between an intersection that contains type parameters (not usable with rest) and one that doesn't (could be usable with rest).
If this is a common scenario we can flatten all-object intersections to object types for now and revisit when we add rest types. But I would prefer to do nothing for 3.1 unless it's important.
…________________________________
From: Bill Ticehurst <[email protected]>
Sent: Saturday, November 26, 2016 10:08:04 PM
To: Microsoft/TypeScript
Cc: Nathan Shively-Sanders; Mention
Subject: [Microsoft/TypeScript] Error using intersection type for object rest destructuring (#12520)
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<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fsandersn&data=02%7C01%7Cnathansa%40microsoft.com%7C0a5d4ea4cefe487a6b0e08d4168bc057%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636158236852606225&sdata=20%2FmBvybZQkSukFXxP1KJ%2FAWxQhsQP7o12DFeI17bZ4%3D&reserved=0> & @mhegazy<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmhegazy&data=02%7C01%7Cnathansa%40microsoft.com%7C0a5d4ea4cefe487a6b0e08d4168bc057%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636158236852616234&sdata=Gf1bAHQIBsY9D6HZbQtY7XamST8TN%2BUo1wfZsfzjGJs%3D&reserved=0> ).
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.
-
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FMicrosoft%2FTypeScript%2Fissues%2F12520&data=02%7C01%7Cnathansa%40microsoft.com%7C0a5d4ea4cefe487a6b0e08d4168bc057%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636158236852616234&sdata=JrstfrKUQqryPzc1eYlBfS6unPsbeMGHC3a%2FQ7ZL82E%3D&reserved=0>, or mute the thread<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAR6Yd0CM42T3JiIj4uFn7DQWqiigFugks5rCR5EgaJpZM4K9Esc&data=02%7C01%7Cnathansa%40microsoft.com%7C0a5d4ea4cefe487a6b0e08d4168bc057%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636158236852616234&sdata=MxXhYSVe78x8cC7N5EPACf6a0fqMcI0YF%2FdJclrxKww%3D&reserved=0>.
|
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). |
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. |
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 ).
The text was updated successfully, but these errors were encountered: