-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Bug 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
S0AndS0, vostrnad, fwienber, whzx5byb, andrewbranch and 4 more
Metadata
Metadata
Assignees
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript