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

Branded string literals revert to string in some cases #60990

Open
mmalerba opened this issue Jan 17, 2025 · 0 comments
Open

Branded string literals revert to string in some cases #60990

mmalerba opened this issue Jan 17, 2025 · 0 comments
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@mmalerba
Copy link

🔎 Search Terms

If I create a branded string literal it seems to respect the string literal in the basic case of assigning to a property, however it loses this information and just reverts to string when assigning to a property or in a template string.

It makes sense that the brand object needs to be dropped in these cases, but rather than converting to string, it should convert to the narrower literal type.

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about branded string

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAsiAyBLYEBOBDANgHgMpQgA8UA7AEwGcoLhVESBzAPigF4p8AyKAbwH0ARhnIAuXgF9xAbgBQAYwD2JGlHRsoAcnQbVVOEhQYcWjU1nylKwmJPr0UqAHpHUAO4LUAawoBCC8uAoEBttdQADABIedHEwh2coADN0RExffxUALzFosRIAVwBbATRxdR4AbXQAXTEADjKE5NTfIA

💻 Code

type MyLiteral<S extends string> = S & {_brand: {}};
const a = 'a' as MyLiteral<'a'>;

const x: 'a' = a; // works!
const y: 'a' = `${a}`; // fails!
const z: {a: number} = {[a]: 8} // fails!

🙁 Actual behavior

For y and z, the types don't match because a is converted to string rather than 'a'

🙂 Expected behavior

The types should match in all 3 cases, x, y, z

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Jan 21, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

2 participants