-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Widened the *From utility types to allow extracting from factory functions #2551
Widened the *From utility types to allow extracting from factory functions #2551
Conversation
🦋 Changeset detectedLatest commit: 6d284fe The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…-from-factory-functions' of https://github.com/mattpocock/xstate into matt/widened-the-from-utility-types-to-allow-extracting-from-factory-functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really useful change. Wondering if it would be too much abstraction to have a type like this?
type FactoryOrValue<T> = T extends ((...args: any[]) => infer U) ? U : T;
Then we can do:
export type StateFrom<T> = FactoryOrValue<T> extends StateMachine<any,any,any>
? FactoryOrValue<T>['transition']
: ...
Just thinking out loud 🤔
We can get this merged in after tests are passing (I can help investigate) |
@@ -828,7 +828,11 @@ export interface StateMachine< | |||
): StateMachine<TContext, TStateSchema, TEvent, TTypestate>; | |||
} | |||
|
|||
export type StateFrom<T> = T extends StateMachine<any, any, any> | |||
export type StateFrom< | |||
T extends |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the idea to add to this extends
whenever we add a new thing that StateFrom
can extract a state from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that will work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this on my branch - https://github.com/mattpocock/xstate/pull/1/files . I don't have super strong opinion about this and definitely having those constraints in place could improve where type errors are reported and so on. We didn't have those in other *From utilities - so I've just gone for the unification for the time being. We can always unify this other way around - although keeping the list of those things is a little bit tedious 🤷♂️
Probably a good idea? I'm really nervous to do stuff like this in a codebase that doesn't have |
@davidkpiano Tried the above, doesn't work |
Re |
Nice; well, we can do refactor later. I think this is good to go in, wdyt? |
Well, sure - but since it's already implemented, wouldn't it be easier to review it, merge to this one and then land on the main branch? |
Sure, that sounds good to me |
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
Copied from changeset:
Widened the *From utility types to allow extracting from factory functions.
This allows for:
This also works for models, behaviours, and other actor types.