Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Intellisense for string/number literals in a widened union #33471

Open
5 tasks done
orta opened this issue Sep 17, 2019 · 11 comments
Open
5 tasks done

Support Intellisense for string/number literals in a widened union #33471

orta opened this issue Sep 17, 2019 · 11 comments
Labels
Experience Enhancement Noncontroversial enhancements Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Suggestion An idea for TypeScript
Milestone

Comments

@orta
Copy link
Contributor

orta commented Sep 17, 2019

Search Terms

autocomplete union wide

Suggestion

In both #28743 and #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.
@RyanCavanaugh
Copy link
Member

keep it broken so people stop writing types like that

@AlCalzone
Copy link
Contributor

There are situations when implementing 3rd party APIS where you know the options that there are right now, but future versions will absolutely add new options.
If you use a closed union type, you get auto completion but errors on every new option that is added. If you use string, you lose auto completion.
How to solve this dilemma?

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Sep 17, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 17, 2019
@fatcerberus
Copy link

fatcerberus commented Sep 17, 2019

I say this as someone who extolls the virtues of self-documenting code constantly...

IMO, this is conflating purely documentary concerns with typing concerns. In reality, the type is just string and we shouldn't pretend otherwise. I understand the value of having an automatic list of suggestions, but that feels more like the domain of documentation-level scaffolding such as JSDoc comments, rather than the type system proper.

@jack-williams
Copy link
Collaborator

type ErrorStatus = (('not_found' | 'failed' | 'parse_error') & tag "suggestions") | string;

@AlCalzone
Copy link
Contributor

Whoa, hold on. Will this actually be a use case of tag types? 😳

@jcalz
Copy link
Contributor

jcalz commented Dec 17, 2019

crosslinking to #29729

@orta orta added the Repro Requested Indicates that we'd like a compiler backed repro for this issue: https://aka.ms/ts-repros label Jul 20, 2020
@typescript-bot typescript-bot added Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros and removed Repro Requested Indicates that we'd like a compiler backed repro for this issue: https://aka.ms/ts-repros labels Jul 20, 2020
@orta orta changed the title Support Intellisense for string/number literals in a widened array Support Intellisense for string/number literals in a widened union Mar 3, 2022
@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 13, 2022

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @orta

❌ Failed: -

  • Argument of type '"not"' is not assignable to parameter of type 'ErrorStatus'.

Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

⚠️ Assertions:

@orta
Copy link
Contributor Author

orta commented Apr 14, 2022

( I've updated the comment which should make the output above make more sens e on the next run )

@orta
Copy link
Contributor Author

orta commented Jan 9, 2023

I was surprised to find a PR to the DOM lib types microsoft/TypeScript-DOM-lib-generator#1467 actually using a side-effect in union handling to get this feature!

// @errors: 1002
type T = "off" | "on" | "name" | (string & {});

declare function t(t: T){}

t("any string")
t("on")

(edit: removed the twoslash repro because it doesn't take completions into account )

@typescript-bot
Copy link
Collaborator

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @orta

❌ Failed: -

  • Unterminated string literal.
  • ')' expected.

Historical Information
Version Reproduction Outputs
4.5.2, 4.6.2, 4.7.2, 4.8.2, 4.9.3

❌ Failed: -

  • Unterminated string literal.
  • ')' expected.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 17, 2024

Search tags: open ended string union literal auto complete

See also #26277

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

8 participants