Assignment operators allow assigning invalid values to literal types
See original GitHub issueBug Report
š Search Terms
addition assignment string literal
append string literal
š Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about string literals
āÆ Playground Link
Playground link with relevant code
š» Code
type Data = { value: 'literal' }
const data: Data = { value: 'literal' }
data.value += 'foo' // Appending a string is possible, despite it being a string literal type.
// data is still of type Data and can be passed along,
// even when the value has the wrong type.
useData(data);
function useData(_: Data): void {}
The same applies to any literal type (e.g. number
). See #48857 for more examples.
š Actual behavior
Appending a random string to the property typed as the literal "literal"
is possible.
š Expected behavior
I would expect an error, saying that the the type "literalfoo"
is not assignable to "literal"
.
Ping @S0AndS0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:22 (5 by maintainers)
Top Results From Across the Web
Python: can't assign to literal - Stack Overflow
You are trying to assign to literal integer values. 1 , 2 , etc. are not valid names; they are only valid integers:...
Read more >Handbook - Literal Types - TypeScript
There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an...
Read more >2. Working with Data: Literals, Values, Variables, and Types
In this chapter we will cover the core data and variable types in Scala. Let's start with the definitions of the terms literal,...
Read more >Assignment operators - cppreference.com
Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are...
Read more >Addition operators - + and += | Microsoft Learn
The C# addition operators (`+`, and `+=`) work with operands of numeric, string, or delegate types.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would personally be very suspicious of any code that switched between members of a finite set of known string literals by way of mutation rather than direct assignment and wouldnāt mind the (hypothetical) error that disallowed it.
My opinion of this pattern for number literals is admittedly more fuzzy though, given that TS doesnāt have range types.
See also https://github.com/microsoft/TypeScript/issues/14745#issuecomment-459881335
This is a bit of a weird spot. Mutation of a value of a single literal type is clearly wrong, but also unlikely to be something you do accidently, so itās a low-value error despite high confidence. However, mutation of a value of a union of literal types is likely to be correct in a way that we canāt statically verify, so itās a false positive.
The natural way to split that would be to say that mutation of non-union literals is disallowed but mutation of union literals is āpresumed OKā, but this is a subtyping violation because an operation thatās allowed on
T | U
should also be valid on aT
or aU
.This code smells really bad, though, and itād be nice to find a more reasonable solution.
OT: To whomever it may concern, please take my personal ensurances that OP understands what they are talking about.