Skip to content

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