Support Intellisense for string/number literals in a widened union
See original GitHub issueSearch Terms
autocomplete union wide
Suggestion
In both https://github.com/microsoft/TypeScript/issues/28743 and https://github.com/microsoft/TypeScript/issues/28743 they were correctly closed for being correctly widened to a string.
Use Cases
I’d like to pitch that this is useful only from the IDE experience, and that’s purely where the value comes in from. The widening to a string makes sense, however allowing this lets you declare an API where you would like to allow any input, but there are preferred options.
Examples
// @errors: 2345 2345
type ErrorStatus = 'not_found' | 'failed' | 'parse_error'
declare function fail(status: ErrorStatus): any
fail("not")
// ^|
declare function failString(status: ErrorStatus | string): any
failString("not")
// ^|
Would offer “not_found”, “failed”, “parse_error” in the completion list.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:14
- Comments:8 (5 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: 1type Sizes = "sm" | "md" | "lg".
Read more >How to get helpful intellisense when using a type alias for a ...
Like many such component libraries, we often use a union of string literals as the type for some props, to ensure they always...
Read more >Playground Example - Intro to Template Literals - TypeScript
TypeScript already supports treating an exact string/number as a literal, for example this function only allows two exact strings and no others:
Read more >typescript string type with specific values
Here we will use the string literal union type in TypeScript. ... Visual Studio Code can help us choose enum values from a...
Read more >TypeScript 3.0: Exploring Tuples and the Unknown Type - Auth0
args is expanded in a way that makes the function signature above ... of a tuple with optional elements is the "union of...
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 Free
Top 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
keep it broken so people stop writing types like that