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

Fix MadeEnum<'a'> to be compatible with MadeEnum<string> #3227

Merged
merged 1 commit into from
Jun 4, 2024

Conversation

CarsonF
Copy link
Member

@CarsonF CarsonF commented Jun 4, 2024

// Given
let Color: MadeEnum<'red' | 'blue'>;
let AnEnum: MadeEnum<string>;

// Now
AnEnum = Color;

Two changes to make this compatible.

First, disallow direct access to member values when referencing generically.

Without this change Color can't be assigned to AnEnum, because the TS defines the member values as an index: { [x: string]: string }. So since one has an index accessor and one doesn't, making them incompatible.

Secondly, The entry() arg type change also allows them to be compatible. Without it TS thinks something like entry() fn accepting any string is not compatible with entry() fn only accepting 'red' | 'blue'. Using another generic at the function level somehow works around this, while still maintaining all the strictness.

Disallow direct access to member values when referencing generically

```ts
let Color: MadeEnum<'red' | 'blue'>;
let AnEnum: MadeEnum<string>;
```
Without this change `Color` can't be assigned to `AnEnum`, because
the TS defines the member values as an index: `{ [x: string]: string }`.
So since one has an index accessor and one doesn't,
making them incompatible.

The `entry()` arg type change also allows them to be compatible.
Without it TS thinks something like `entry()` fn accepting any `string`
is not compatible with `entry()` fn only accepting `'red' | 'blue'`.
Using another generic at the function level somehow works around this,
while still maintaining all the strictness.
@CarsonF CarsonF requested a review from a team June 4, 2024 15:14
Copy link
Contributor

@brentkulwicki brentkulwicki left a comment

Choose a reason for hiding this comment

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

This all makes sense, but my curiosity wants to know a real life example of where this will be handy in our codebase? Or are you optimizing for the future here?

@CarsonF
Copy link
Member Author

CarsonF commented Jun 4, 2024

This all makes sense, but my curiosity wants to know a real life example of where this will be handy in our codebase? Or are you optimizing for the future here?

PR incoming for Workflow, which will have a state enum, typed generically.

@CarsonF CarsonF merged commit 6910f5c into develop Jun 4, 2024
15 checks passed
@CarsonF CarsonF deleted the generic-enum branch June 4, 2024 17:55
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.

2 participants