Skip to content

Completion should prefer literal types in union types #13614

Description

@notoriousb1t

TypeScript Version: 2.2.0-dev.20170120

Code

interface ICssProperties {
    display: 'inline-block' | string
}

const className: ICssProperties = {
    display: ''
}

Expected behavior:
The auto completion list shows 'inline-block' followed by general string suggestions

Actual behavior:
The auto completion list shows general string suggestions none of which are 'inline-block;

Activity

  1. notoriousb1t commented on Jan 21, 2017

    @notoriousb1t
    Author

    Andy (Andrewkraft) (@Andy-MS), from your comment in the other thread, it sounds like someone chose to make a regression in tooling in exchange for a compiler optimization. For a language like TypeScript where one of its largest features is good tooling, I think this is a mistake.

    The TypeStyle project leverages literal types to provide good auto-completion for css property values. A lot of properties have exact keywords for which type ahead support is great. However, a lot of these can also take units like 3px which can't be described to the type system, so we have to fallback to a string.

    Here is an issue recently brought by a developer using TypeStyle: typestyle/typestyle#104

    What would it take to restore this behavior?

  2. rozzzly commented on Jan 22, 2017

    @rozzzly

    I agree with B1t (@notoriousb1t) . This is a feature I really want; there's tons of instances where it can make writing quality code quicker—one of the things about TypeScript that makes the DX so great. Granted, the spec does denote that in such cases the union would get widened to string, yet, those suggestions are still highly relevant. Can't there be some convenient deviation from the what the type checker derives.

    So all we see when querying the type checker for the type of x is string. So, this can't be fixed.
    — Andy (Andrewkraft) (@Andy-MS)

    That's such a cop-out man. I'm not familiar with the internals of the type checker, but surely some metadata could be supplied to the completion provider alongside the results from the type checker.

  3. locked and limited conversation to collaborators on Jun 19, 2018
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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions