question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Assignment operators allow assigning invalid values to literal types

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
fatcerberuscommented, Dec 8, 2021

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.

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.

6reactions
RyanCavanaughcommented, Dec 7, 2021

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 a T or a U.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found