Skip to content

Display narrowed type of properties in completionEntryDetails #51526

@DanielRosenwasser

Description

@DanielRosenwasser
interface Palette {
    red?: Color;
    green?: Color;
    blue?: Color;
}

type Color = string | [number, number, number];

function checkOrange(p: Palette) {
    if (typeof p === "object" && "orange" in p && typeof p.orange === "number") {
        p./**/
    }
}

Request completions at /**/ and select the option for orange.

Today, we show
(property) orange: unknown

image

[
    {
        "name": "orange",
        "kindModifiers": "",
        "kind": "property",
        "displayParts": [
            {
                "text": "(",
                "kind": "punctuation"
            },
            {
                "text": "property",
                "kind": "text"
            },
            {
                "text": ")",
                "kind": "punctuation"
            },
            {
                "text": " ",
                "kind": "space"
            },
            {
                "text": "orange",
                "kind": "propertyName"
            },
            {
                "text": ":",
                "kind": "punctuation"
            },
            {
                "text": " ",
                "kind": "space"
            },
            {
                "text": "unknown",
                "kind": "keyword"
            }
        ],
        "documentation": [],
        "tags": []
    }
]

This has to do with how we decide to display properties - we simply request the type of what p is narrowed to, not the type of individual properties. This is actually part of another weirder distinction, where narrowing a property doesn't also narrow the containing value's type.

Recently there's been some discussion around whether we could do that; however, I am a bit skeptical here. Short of that, it would be nice to show users the actual type they're using.

This could get a little bit expensive; but narrowing in general should get cheaper. In theory, #51525 could avoid much of the work, and make the overhead here very low.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: LS: Type DisplayBugs relating to showing types in Quick Info/Tooltips, Signature Help, or Completion InfoIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions