Skip to content
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

Fixed an issue with typegen types not being able to provide events that had a union of strings as their type #3230

Merged
merged 2 commits into from
Apr 19, 2022

Conversation

Andarist
Copy link
Member

@changeset-bot
Copy link

changeset-bot bot commented Apr 16, 2022

🦋 Changeset detected

Latest commit: 485a373

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Patch

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

@ghost
Copy link

ghost commented Apr 16, 2022

CodeSee Review Map:

Review these changes using an interactive CodeSee Map

Review in an interactive map

View more CodeSee Maps

Legend

CodeSee Map Legend

Comment on lines 8 to 14
"typesVersions": {
"<4.1": {
"lib/types.4.1.d.ts": [
"types.pre-4.1.d.ts"
]
}
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to verify in some external project that this will be resolved correctly

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 16, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 485a373:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

@@ -15,7 +22,8 @@
"lib/**/*.js",
"lib/**/*.d.ts",
"es/**/*.js",
"es/**/*.d.ts"
"es/**/*.d.ts",
"*.d.ts"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this one needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plan was to keep types targeting lowe versions of TS in this file and that would have to be published. However, it seems that it's not possible to redirect internal file requests (see here) so this strategy won't work.

To make it happen we would have to bundle all of our types, replace in it what we want, and point to that from typesVersions. It's way more work than what I've wanted to pull off here so I'm going to just revert this part of the change (optimization) and keep it in a TODO comment for v5.

export type IndexByType<T extends { type: string }> = {
[K in T['type']]: Extract<T, { type: K }>;
[K in T['type']]: T extends any ? (K extends T['type'] ? T : never) : never;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why T extends any makes this work 🧐

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/statelyai/eng-blog/pull/81/files :P

This is just a way to make this conditional type distributive. Conditional types are distributive when they contain a "naked" type param:

type IsStringDistributive<T> = T extends string ? 1 : 0

// this will run the conditional type over each member of this union type
type Distributes = IsStringDistributive<string | number>
//   ^? 1 | 0

type IsStringNonDistributive<T> = [T] extends [string] ? 1 : 0

// this will run the conditional type on the union type "as is"
type DoesntDistribute = IsStringNonDistributive<string | number>
//   ^? 0

TS playground
TS playground with Omit variants

So this particular type here just checks if the current K extends any of the T['type'] and it will match even if T['type'] is a union. Whereas the previous version would fail because

'FOO' | 'BAR' extends 'FOO' ? 1 : 0 // 0

@Andarist Andarist merged commit 780458c into main Apr 19, 2022
@Andarist Andarist deleted the andarist/fix-3102 branch April 19, 2022 12:12
@github-actions github-actions bot mentioned this pull request Apr 19, 2022
nevilm-lt pushed a commit to nevilm-lt/xstate that referenced this pull request Apr 22, 2022
…at had a union of strings as their `type` (statelyai#3230)

* Fixed an issue with typegen types not being able to provide events that had a union of strings as their `type`

* Remove `typesVersions` attempt from the core
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: typegen types resolving events to never in guards/actions
2 participants