Skip to content

suggestion: LiteralUnion #2

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Nov 29, 2024

Conversation

xDivisionByZerox
Copy link
Contributor

Description

I recently watched your video on "Fix the Fetch Type" and noticed that you implemented the MIME types for the "Content-Type" and "Accept" headers as a union of MIME types. While this approach is effective, you mentioned during the creation of the "TypedHeaders" type that you appreciate having auto-completion for some headers while allowing custom ones to be present as well.

Therefore, I wanted to bring to your attention that a similar approach can be applied to strings as well.

Please feel free to close this PR if you are already aware of this solution.

Explanation

Firstly, I would like to clarify that this is not my original work but was discussed (and solved) in a TypeScript issue back in 2019: microsoft/TypeScript#29729 (comment).
The linked issue-comment also provides an explanation of why/how this works:

In this code-snippet the value will be of type Suggestion | (string & {}) because though the union type Suggestion inherit string, it does not inherit string & {}, which means Suggestion and string & {} are treated as distinguishable types.

@chwoerz chwoerz merged commit 7cdeda0 into typed-rocks:main Nov 29, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants