-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Support proposed ES Rest/Spread properties #2103
Comments
We'd probably need some examples of how this could plausibly be emitted in downlevel (if that's in scope). |
@RyanCavanaugh, I added some little emitting example and typing idea. |
I recently discovered this feature in ES7 via this article. I have to say, it's quite handy! Would love to see this in TypeScript! |
Any updates on this? Would love to see this in 1.6 as it would come super handy when using with React 😄 |
@prabirshrestha for the record, we currently have the property spread operator for JSX on |
@DanielRosenwasser Could you elaborate? Does that mean this works on master with the --jsx flag? Does that include rest properties or just spread properties? |
Sorry @mnpenner and @prabirshrestha, yes, I meant on |
JSX supports spread attributes, e.g. |
I'm very interested in both Redux and React, the manipulation in state is made much easier with this. Would love to see this implemented. |
Rest/Spread is approved for Stage 2 now https://github.com/tc39/tc39-notes/blob/master/es7/2015-07/july-30.md |
We want to wait for the proposal to reach Stage 3 before addressing this. |
It's very useful in react and redux , please implement it as soon as possible , please :-) |
Here's a helper module I've been using to work around this. It's not ideal as you have to specify all the keys twice, once on the left hand side and once as strings on the right hand side, but I can't think of a better way to do it. Any feedback welcome, I'm sure there's edge behaviour I've missed! |
Is this something you'd accept a patch for? (to be merged once it reaches stage 3). And if so, have any pointers of where to start making changes to support it? This is the last ES7+ feature that my team heavily uses that's preventing us from switching over to typescript; would definitely love to have to sooner, if possible. |
We would consider a PR here. One note though, wiring the type system for this feature is not a simple task. If you are interested in perusing this I would keep it incremental, and would start by parsing then emit then type checking. @sandersn should be able to help you if you have any questions. |
Just a note for those interested in using this with Redux. I agree the notation is convenient. But I've actually been getting by just fine without it by leverage the In any case, 👍 for spread operators in TS. 😄 |
@xogeny I would be very interested in having a look at that library. Thanks |
@cur3n4 I have a repository where I've been playing with these ideas. It changes quite a bit (since this is all still evolving). I was using a special modules that leveraged But don't get me wrong, I still think a spread operator would be really useful. It would actually allow you to express and do things safely and tersely in the language that I don't see how to do with the current type system (i.e., allowing you to describe the right type constraints on external libraries). For the moment, I sacrifice the terseness for the safety (while the |
After using Babel for past few months, converted my project to TS and had to go and redo all these. Would love to see this implemented! |
It seems like the proposal has reached stage 3: https://github.com/tc39/proposals |
@ddaghan your example with tsx won't work |
any timeframe for this feature? |
Some one should update the Roadmap |
It seems that using this feature allows the assignment of unknown props: interface State {
a: string;
b: number;
}
let state: State = { a: "a", b: 0 };
state = {...state, x: "x"}; // No error I was expecting an error that For example, my current workaround before this PR was this: state = update(state, { x: "x" }); // Error: Property 'x' is missing in type 'State'.
function update<S extends C, C>(state: S, changes: C): S {
return Object.assign({}, state, changes);
} Is it possible to achieve this with object spread/rest? |
Object Rest and Spread, as per the ES proposal, behaves similar to |
But that's what I'm confused about: |
sorry.. object literals are a special case. my example was wrong. here is a better example: let obj = { a: "a", b:0, x: "X" };
let state: State = obj; // OK The issue here is if rather subjective. When the compiler sees however, if you spread an object, let's say that said. we should reconsider this decision. filed #12717 to track that. |
I see. I love your idea in #12717, that's exactly what I was thinking. I would actually like such behavior even for the spread props but I can see your point that it's very subjective and might be annoying for some common JS use cases. |
@aaronbeall I agree, it would be annoying for common JS use cases... Most of the time, you just want to ensure the object has the shape of the interface specified and not directly inspect the spread object, the current implementation is okay IMO |
Hi guys, Congrats for the great release! Now is time for a deserved rest... speacking of whitch I've an issue with the rest operator :) Using React you tipically have a component like: export interface MyLinkProps extends React.HTMLAttributes {
myUrl: string
}
class MyLink{
render(){
const {myUrl, ...attrs } = this.props;
return <a href={calculate(myUrl)} ...attrs>Click here</a>;
}
} The issue is that when you hover with the mouse over I know that typescript is structurally typed and all that, but could be possible to assign an explicit type to the rest variable? Some alternatives: const {myUrl, ...attrs as React.HtmlAttributes } = this.props; //Is not really a casting
const {myUrl, ...attrs : React.HtmlAttributes } = this.props; //conflicts with ES6?
const attrs : React.HTMLAttributes;
const { myUrl, ...attrs } = this.props; //re-declare the variable |
@bondz Well, its not true in 100% of the uses in my project. :) But in another project it may be very annoying. In my case I'm using Redux and React and making heavy use of immutable state, which means to update or create state objects I must copy all props onto a new object literal... in all cases I want 100% type safety that I'm trying to copy the right data onto the target state interface. Currently I use functions to ensure this safety, but I would prefer to use object spread because its cleaner (readable, expressive, standard syntax.) But in someone else's project they might want different behavior because they are using a lot of untyped or loose object structures, so I see how it's quite subjective. I think the #12717 suggestion is a good middle ground (and more consistent with existing object literal type safety.) |
Seems it's already state 3, any plan of supporting this? Btw, we use VSCode mainly for ES development, not TS yet :) |
@evisong this feature has shipped and is already available in the latest version of vsCode. Update your vsCode to version 1.8 |
es7 proposal : https://github.com/sebmarkbage/ecmascript-rest-spread
Spread properties
Typing
In my opinion the goal of this method is to be able to duplicate an object and changing some props, so I think it's particularly important in this case to not check duplicate property declaration :
I have a very naive type check algorithm for a similar feature (
JSXSpreadAttribute
) in my littlejsx-typescript
fork: I just copy the properties of the spread object in the properties table when I encounter a spread object, and override those property if I encounter a declaration with a similar name.Emitting
jstransform use
Object.assign
, babel introduce a shim:We could either force the presence of
assign
function onObjectConstructor
interface, or provide a similar function (with a different name).I think that the optimal solution would be to not emit any helper in
es6
target (or ifObject.assign
is defined), and to emit an helper function fores5
,es3
.Rest properties
Typing
For simple object the new type is a subtype of the assignation that does not contains properties that has been captured before the rest properties :
If the destructuring assignment has an index declaration, the result has also a similar index declaration:
new/call declarations are obviously not captured:
Emitting
It is not possible to emit rest properties without an helper function, this one is from babel:
Edit: added some little typing/emitting example
The text was updated successfully, but these errors were encountered: