Literal String Union Autocomplete
See original GitHub issueAutocomplete 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:
Actual behavior:
Playground Link: https://stackblitz.com/edit/typescript-bwyyab
Issue Analytics
- State:
- Created 5 years ago
- Reactions:166
- Comments:51 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
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
In this snippet
a
will have typestring
because both"hello"
and"world"
inheritstring
.In this code-snippet
a
will be of type"hello" | "world" | (string & {})
because though both"hello"
and"world"
inheritstring
, they do not inheritstring & {}
, which means"hello" | "world"
andstring & {}
are treated as distinguishable types.Hope this helped you understanding.
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:
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!