镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@orta

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.

Activity

  1. RyanCavanaugh commented on Sep 17, 2019

    @RyanCavanaugh
    Member

    keep it broken so people stop writing types like that

  2. AlCalzone commented on Sep 17, 2019

    @AlCalzone
    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?

  3. fatcerberus commented on Sep 17, 2019

    @fatcerberus

    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.

  4. jack-williams commented on Sep 18, 2019

    @jack-williams
    Collaborator
    type ErrorStatus = (('not_found' | 'failed' | 'parse_error') & tag "suggestions") | string;
  5. AlCalzone commented on Sep 18, 2019

    @AlCalzone
    Contributor

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

  6. jcalz commented on Dec 17, 2019

    @jcalz
    Contributor

    crosslinking to #29729

  7. added
    Repro RequestedIndicates that we'd like a compiler backed repro for this issue: https://aka.ms/ts-repros
    on Jul 20, 2020
  8. added
    Has ReproThis issue has compiler-backed repros: https://aka.ms/ts-repros
    and removed
    Repro RequestedIndicates that we'd like a compiler backed repro for this issue: https://aka.ms/ts-repros
    on Jul 20, 2020
  9. changed the title [-]Support Intellisense for string/number literals in a widened array[/-] [+]Support Intellisense for string/number literals in a widened union[/+] on Mar 3, 2022
  10. typescript-bot commented on Apr 13, 2022

    @typescript-bot
    Contributor

    👋 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 Therox (@orta)

    ‼️ Exception: Error - error TS5107: Option 'moduleResolution=node10' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error. Visit https://aka.ms/ts6 for migration information.

    Error: error TS5107: Option 'moduleResolution=node10' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
      Visit https://aka.ms/ts6 for migration information.
    
        at Object.createVirtualTypeScriptEnvironment (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:8128:11)
        at twoslasher (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:7618:17)
        at /home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:439:44
        at runTwoslashRequests (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:406:56)
        at run (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:20096:75)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    

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

    ⚠️ Assertions:

    ⚠️ Way slower
  11. orta commented on Apr 14, 2022

    @orta
    ContributorAuthor

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

  12. 18 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experience EnhancementNoncontroversial enhancementsHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions