Typescript: Completion should prefer literal types in union types

Created on 21 Jan 2017  Â·  3Comments  Â·  Source: microsoft/TypeScript

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;

Duplicate

Most helpful comment

@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: https://github.com/typestyle/typestyle/issues/104

What would it take to restore this behavior?

All 3 comments

Duplicate of #12687.

@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: https://github.com/typestyle/typestyle/issues/104

What would it take to restore this behavior?

I agree with @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-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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uber5001 picture uber5001  Â·  3Comments

dlaberge picture dlaberge  Â·  3Comments

manekinekko picture manekinekko  Â·  3Comments

blendsdk picture blendsdk  Â·  3Comments

bgrieder picture bgrieder  Â·  3Comments