-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
🔎 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
💻 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
MichalMarsalek, njakob and mkantor
Metadata
Metadata
Assignees
Labels
Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases