-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Display property types in optional chains without optional type marker #53804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4d5241e
81934f5
227bb9b
d107980
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// <reference path='fourslash.ts'/> | ||
// | ||
// @strict: true | ||
// | ||
//// interface A { | ||
//// arr: string[]; | ||
//// } | ||
//// | ||
//// function test(a?: A): string { | ||
//// return a?.ar/*1*/r.length ? "A" : "B"; | ||
//// } | ||
//// | ||
//// interface Foo { bar: { baz: string } }; | ||
//// declare const foo: Foo | undefined; | ||
//// | ||
//// if (foo?.b/*2*/ar.b/*3*/az) {} | ||
//// | ||
//// interface Foo2 { bar?: { baz: { qwe: string } } }; | ||
//// declare const foo2: Foo2; | ||
//// | ||
//// if (foo2.b/*4*/ar?.b/*5*/az.q/*6*/we) {} | ||
|
||
verify.quickInfoAt("1", "(property) A.arr: string[]"); | ||
verify.quickInfoAt("2", "(property) Foo.bar: {\n baz: string;\n}"); | ||
verify.quickInfoAt("3", "(property) baz: string | undefined"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused as to the end result here; both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I admit this is a little bit confusing and I'm open for feedback how to improve this further.
OTOH, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that makes sense, though I'd probably personally defer to @DanielRosenwasser 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the intuition described is fine, but this example kind of shows some inconsistencies in this approach where method return types don't exhibit the same behavior. Whether that is enough of a blocker, I don't know. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good catch. I think that it's worth unifying those - I'd expect I think that this find doesn't have to block this PR, I can just prepare a follow up fixing it at some later point in time. Up to you though. |
||
verify.quickInfoAt("4", "(property) Foo2.bar?: {\n baz: {\n qwe: string;\n };\n} | undefined"); | ||
verify.quickInfoAt("5", "(property) baz: {\n qwe: string;\n}"); | ||
verify.quickInfoAt("6", "(property) qwe: string | undefined"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not that familiar with this part of the code. Why doesn't this also apply to the entirety of
a?.b.c
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "optional type" marker is not added at the outermost
PropertyAccessExpression
, only for inner optional chain parts. At the outermostPropertyAccessExpression
, we just union it with the normalundefined
type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See
propagateOptionalTypeMarker
in checker.ts for the logic related to when the marker type is added vs a regularundefined
.