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.

Literal String Union Autocomplete

See original GitHub issue

Autocomplete works for literal string unions, but adding a union of string negates autocomplete entirely. This has been brought up before but I believe there is enough value in this feature to be reconsidered.

My use case is to have a union of string literals for several colors, but also allow hex codes without having to add 16.7 million string literals.

TypeScript Version: 3.4.0-dev.20190202

Search Terms: Literal string union autocomplete

Code

interface Options {
  borderColor: 'black' | 'red' | 'green' | 'yellow' | 'blue' | string
};

const opts: Options = {borderColor: 'red'};

Expected behavior:

image

Actual behavior:

image

Playground Link: https://stackblitz.com/edit/typescript-bwyyab

Related Issues: #12687 #13614

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:166
  • Comments:51 (7 by maintainers)

github_iconTop GitHub Comments

60reactions
manuthcommented, May 25, 2020

@AhmedElywa it’s because you’re using (U & never).
Edit: After a year or something I finally noticed that my very own example was incorrect… sorry, pal 😅😂

Here’s the longer explanation:

let x: (string & never); // x has type `never`
let y: number | (string & never); // y has type `number`
let z: ("Hello" | "world") | (string & never); // z has type `"Hello" | "world"`

In order to get the solution to work you have to use U & {}. Following might give you an idea why this solution works.

How the solution works

let a: ("hello" | "world") | string;

In this snippet a will have type string because both "hello" and "world" inherit string.

let a: ("hello" | "world") | (string & {});

In this code-snippet a will be of type "hello" | "world" | (string & {}) because though both "hello" and "world" inherit string, they do not inherit string & {}, which means "hello" | "world" and string & {} are treated as distinguishable types.

Hope this helped you understanding.

41reactions
manuthcommented, May 25, 2020

Hey Guys Though the issue still isn’t solved but I found out that in TS 3.5.1 you don’t even have to create some weird property in order to get the workaround done:

type LiteralUnion<T extends U, U = string> = T | (U & {});
let x: LiteralUnion<"hello" | "world">;
x = "hey";

While this code is perfectly valid, “hello”, and “world” are still showing up in the autocompletion. Thank you for this great fix, @RyanCavanaugh and @spcfran!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flexible Types that Support Autocomplete with Template ...
In Typescript, when defining a type, it's nice to use a union of literals: ... 2type Sizes = LiteralUnion<"sm" | "md" | "lg",...
Read more >
Autocomplete in typescript of literal type and string [duplicate]
... it is a "different" string type that literal string types are not assignable to (and thus the union won't get reduced to...
Read more >
Does literal union autocomplete in the template? : r/Angular2
I am running angular 13.1.1 but string literals do not autocomplete in the template. I saw the feature request…
Read more >
Haz on Twitter: "TypeScript protip: accept any string, but ...
TypeScript protip: accept any string, but suggest some specific values on autocomplete.
Read more >
String Literal Union Type with TypeGuard in Typescript
It can be anything you want, and it help you to select the correct value for the variable in the typescript. type-autocomplete. Typescript...
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