Open
Description
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
(property) orange: unknown
[
{
"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.