-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
Allow narrowing {}
originating in unknown
using instanceof
#51014
Changes from all commits
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 |
---|---|---|
|
@@ -16,3 +16,16 @@ function f(x: {}, y: unknown) { | |
} | ||
y; // {} | ||
} | ||
|
||
|
||
// repro #51007 | ||
function isHTMLTable(table: unknown): boolean { | ||
return !!table && table instanceof Object && 'html' in table; | ||
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 the test here should actually test for an access of function tryGetHtmlPropFromTable(table: unknown) {
if (!!table && table instanceof Object && 'html' in table && table.html instanceof Object) {
return table.html;
}
return 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. hm, what would it change here? the reported error was that 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. The original test is fine; having another test that actually tries to narrow and use the newly-tested property corresponds to something closer to real-world usage. 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. Ok, I've pushed out the proposed test case - please take a look if this is what you had in mind here. |
||
} | ||
|
||
function tryGetHtmlPropFromTable(table: unknown) { | ||
if (!!table && table instanceof Object && 'html' in table && table.html instanceof Object) { | ||
return table.html; | ||
} | ||
return 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.
The problem with conditioning this on the declared type is that it doesn't work when the empty anonymous object type originates in some other type. For example: