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.

Support Intellisense for string/number literals in a widened union

See original GitHub issue

Search 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:open
  • Created 4 years ago
  • Reactions:14
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

24reactions
RyanCavanaughcommented, Sep 17, 2019

keep it broken so people stop writing types like that

4reactions
jack-williamscommented, Sep 18, 2019
type ErrorStatus = (('not_found' | 'failed' | 'parse_error') & tag "suggestions") | string;
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: 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 >

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