-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Addition of string (or template) literal types should result in a string literal type #51583
Comments
Duplicate of the very old #13969. |
@MartinJohns most of that seems to specifically be talking about template literal strings - which seems like a great idea, that I didn't consider for my issue! I do see one or two comments mentioning addition, but most seem to talk about template literals. |
The templates are probably the "similar expressions", but the title also mentions "literal types for string concatenations" (also in several examples in the comments). |
Also worth noting (as I just commented in that issue) if you add |
We need there to be some way to opt out of this behavior since this is potentially combinatorially explosive (imagine writing |
Couldn't that be like assert one of the summands to string? That would make it an explicit opt-out instead of an explicit opt-in like template literal strings.
I would agree that the usecase of "I want to concatenate string literal types" is addressed! But in terms of addressing the usecase of better typing for code as it's written - there is a gap. Using template literals to concentrate strings is a stylistic choice which many codebases don't opt-in to unless there's string literals involved (I.e. |
Problem is that if you infer |
In principle no, because there's no reason not to construct the type |
This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
π Search Terms
string literal type template addition sum add
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I was playing around today and I noticed that if I add two string literals together, TS does not understand that the result of adding two string literal types is always going to be another string literal type.
π Motivating Example
playground
Maybe even including number literal types and boolean literal types would be good as well? Eg
playground
π» Use Cases
I don't have any real usecases, I just thought it would be a neat property to have in the type system rather than always widening the type to
string
.One case that it would improve would be computed class/object members:
playground
As an extension - if you were brave you could probably also do the same for adding number literal types eg:
playground
or bigint types:
playground
The text was updated successfully, but these errors were encountered: