Skip to content

Syntax for hinting literal type inference #10195

Description

Now that we have so many literal types we more than ever need new syntax that would make their use natural. Please consider the following:

const value = (true); // true
const value = true; // boolean

const value = ('a'); // 'a'
const value = 'a'; // string

const value = (1); // 1
const value = 1; // number

const value = ['a', 1]; // (string | number)[]
const value = (['a', 1]) // [string, number]
const value = ([('a'), (1)]) // ['a', 1];

Problem:

  • There is no way to get a literal type value without explicit type annotation.

Solution:

  • Add new syntax (or shall we say some new semantics to the old mostly unused syntax)

Highlights:

  • Composable.
  • Works fine with the existing syntax, doesn't need a new one.
  • Highly unlikely to be a breaking change.
  • Doesn't affect JavaScript semantics at all, can be executed as written and will work as expected.

Shortcomings:

  • Undiscoverable syntax.
  • Semantic conflicts (breaking changes) although rare:
    • generated code
    • conditional expressions
    • unintended excessive syntax

Prior work:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedIn 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