Skip to content

Assignment operators allow assigning invalid values to literal typesΒ #47027

@MartinJohns

Description

@MartinJohns

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions