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

Assignable TemplateLiteral of Intersection Type of Primitive and {} to Primitive String #61174

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

Conversation

horita-yuya
Copy link

Fixes #60990 (partially)

We can use a technique called branded types by defining a type as primitive & { brand: "branded" }.

This has the benefit of allowing you to handle the target primitive type more constrainedly. For example, with 1 & { brand: "branded" } type, it can only be used in a narrower context than the literal type 1.

The relationship between the types is:

1 & { brand: "branded" } < 1 < number

In other words, 1 & { brand: "branded" } can be assigned to a 1 type, and 1 can be assigned to a number type.

Wouldn't it be more natural to handle Template Literals similarly?

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Feb 13, 2025
@horita-yuya
Copy link
Author

@microsoft-github-policy-service agree

@horita-yuya horita-yuya changed the title Treat primitive and object Intersection Type inside TemplateLiteral as primitive Assignable TemplateLiteral of Intersection Type of Primitive and {} to Primitive Feb 15, 2025
@horita-yuya horita-yuya changed the title Assignable TemplateLiteral of Intersection Type of Primitive and {} to Primitive Assignable TemplateLiteral of Intersection Type of Primitive and {} to Primitive String Feb 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Branded string literals revert to string in some cases
2 participants