Skip to content

Update LiteralUnion type definition so it doesnt cast everything to string #1832

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mi5ha
Copy link

@mi5ha mi5ha commented Oct 30, 2024

If LiteralUnion is defined like this:

export type LiteralUnion<T extends U, U = string> = T | (U & {});

It will make these have string type:

EvaStatus
EvaSize
EvaInputSize

So information about union is lost.

This is possible fix:

export type LiteralUnion<T extends U, U = string> = T | (string & {});

Now EvaSize and other will be of proper union type, and they will also allow custom string.

This fix will also make autocomplete possible for those props.

@@ -8,7 +8,7 @@ export type ChildrenWithProps<Props> = ChildrenProp<ReactElement<Props>>;
* https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export type LiteralUnion<T extends U, U = string> = T | (U & {});
export type LiteralUnion<T extends U, U = string> = T | (string & {});

Choose a reason for hiding this comment

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

👋 @mi5ha,

This won't work in the case you wanna change U to number for example (there you should get a type error).
image

So I think changing this type to the following would be more correct, as you then get ts errors in the numbers case, while also receiving auto-completion hints.

type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);

image
image

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