-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Object type spread #1326
Comments
This would be incredibly useful to us, we extend types all the time just for arguments. This is really nice if a function has defaults, but then we want all functions called by that to actually have a proper object with all missing attributes filled in. A simple use case: type OptionsInput = {
a: number,
b: ?string,
c: Date
};
type FnOptions = {
...OptionsInput,
b: string
};
function foo(opts: OptionsInput) {
if (!opts.b) opts.b = 'some default';
doSomethingWith(opts);
} |
@STRML in this case intersection should probably work |
@vkurchatkin It doesn't. For example, I'm intersecting such an optional property with a required property: 16: keys?: Array<string>,
^^^^^^^^^^^^^ undefined. This type is incompatible with
22: keys: Array<string>,
^^^^^^^^^^^^^ array type For the record, this simply failed without warning in 0.20.1 - appears to have been coercing to any. |
Another thought: Even in cases where intersection would work, the spread syntax would enable an easier-to-read shorthand. For example, to include common types in a disjoint union: type Todo = {
id: string,
text: string,
};
type Action =
| (
{
type: 'CREATE_TODO',
} & Todo
)
| {
type: 'DELETE_TODO',
id: string,
}
; With spread: type Todo = {
id: string,
text: string,
};
type Action =
| {
type: 'CREATE_TODO',
...Todo,
}
| {
type: 'DELETE_TODO',
id: string,
}
; |
Any status on this? This would be a huge help in defining so many types. |
This would be useful to type database schemas. Depending on how you perform queries, a field that represent a relationship with another table / document may return an id or an object (if you're asking for a join).
This works, but forces you to type check the
Would love to hear how you guys handle database / ORM stuff. |
Any update on this? It would be very useful to handle the very common use case described by @STRML |
👍 |
Proposal: additional syntax: Here's the use case:
Of course while this example is simplistic, once you have a class which receives 20 named properties it is currently a major DRY violation when it becomes necessary to duplicate type declarations for all types which have defaults. Another major advantage here is that if you define a class that extends Base, its constructor can now be well defined!
For completeness |
Another note on the usefulness of this feature: currently using intersection types the resultant errors can be rather indecipherable, displaying full lists of all the intersecting types. Simplified error messaging for spreads should be possible, given that a type mismatch will now be traceable to a single concrete property definition and never to something strange like an uninhabitable union, which is a problem that is fundamentally split between definitions in two locations. |
I'm thinking of trying to tackle the code for this. Does anyone have thoughts about what the limitations of the spread operator should be? How should it treat a spreaded object type that has an indexer or a callable? Should it be able to spread unsealed object types? What should the behavior be? Should it be able to spread intersection of object and/or union of object types? |
@conartist6, I think you can do something like
But you need to watch out for #2674. |
That was one of the first things I looked at, but it is actually not correct! The problem is that it is creating optional values not optional keys. |
This seems good to close! |
This may need to be reopened, I don't think the current implementation is what @samwgoldman proposed- see this example The example above shows that type A = {
foo: string,
bar: number,
};
type B = {
bar: boolean,
};
type C = {
...A,
...B,
}; Does not resolve |
@seveibar See #3534. You can work around this by using exact types for now. This is an indication of how unintuitive non-exact object types are, and how something like #3214 is probably a good idea. |
Due to intersection types being incredibly instrusive, we opt to ignore the typing for now. The right way would be to spread the object types, but currently the implementation is buggy. See: facebook/flow#1326
Due to intersection types being incredibly instrusive, we opt to ignore the typing for now. The right way would be to spread the object types, but currently the implementation is buggy. See: facebook/flow#1326
Due to intersection types being incredibly instrusive, we opt to ignore the typing for now. The right way would be to spread the object types, but currently the implementation is buggy. See: facebook/flow#1326
* Refactor TimetableContainer by seperating action row buttons * Add CSS Modules support * Refactor timetable * Temporarily ignore flow type errors Due to intersection types being incredibly instrusive, we opt to ignore the typing for now. The right way would be to spread the object types, but currently the implementation is buggy. See: facebook/flow#1326 * Fix global styling issues * Fix production build issues * Minor stylistic changes
@STRML I get |
Be sure your babel version is up to date for that. |
Just tried 6.26.0 and got the same issue. As far as I can tell, that's the latest version of babel, right? |
Oh right. I think it's only been merged into the 7.x branch.
Mark Siebert wrote:
…
Just tried 6.26.0 and got the same issue. As far as I can tell, that's
the latest version of babel, right?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1326 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABJFPyrhAgaXo9k7JjwcMOYOvuBm5Lleks5st8hogaJpZM4HN5PG>.
|
Cool. Thanks for your help. 👍 I'll just stick with the comment syntax for now. |
@conartist6 you can do what you asked here using a combination of spreading and $Shape like follows:
|
Does object type spread work?
Problem doesn't happen with exact object shapes but I don't get why there should be a problem with inexact object shapes. |
@jedwards1211 it works fine in |
all released versions I've tried don't work though, going back a long way. So I'm not sure whether it's a bug or something I fundamentally misunderstand about how object type spread is intended to work |
it's long standing bug |
It would be nice to be able to spread an existing object type into another one:
In the above,
C
would resolve to{ foo: string, bar: boolean }
.This is different from intersection types. With intersection types,
A & B
becomes something more like{ foo: string, bar: number & boolean }
, which the observant reader will notice is uninhabitable.The text was updated successfully, but these errors were encountered: