TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Support Intellisense for string/number literals in a widened union

Open orta opened this issue 6 years ago • 11 comments

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:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.

orta avatar Sep 17 '19 18:09 orta

keep it broken so people stop writing types like that

RyanCavanaugh avatar Sep 17 '19 18:09 RyanCavanaugh

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?

AlCalzone avatar Sep 17 '19 18:09 AlCalzone

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.

fatcerberus avatar Sep 17 '19 23:09 fatcerberus

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

jack-williams avatar Sep 18 '19 14:09 jack-williams

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

AlCalzone avatar Sep 18 '19 14:09 AlCalzone

crosslinking to #29729

jcalz avatar Dec 17 '19 15:12 jcalz

:wave: 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

:x: 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

:warning: Assertions:

typescript-bot avatar Apr 13 '22 23:04 typescript-bot

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

orta avatar Apr 14 '22 14:04 orta

I was surprised to find a PR to the DOM lib types https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/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 )

orta avatar Jan 09 '23 21:01 orta

:wave: 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

:x: 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

:x: Failed: -

  • Unterminated string literal.
  • ')' expected.

typescript-bot avatar Jan 10 '23 08:01 typescript-bot

Search tags: open ended string union literal auto complete

See also #26277

DanielRosenwasser avatar Jan 17 '24 21:01 DanielRosenwasser