Skip to content
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

Support for Object.hasOwn narrowing #71

Draft
wants to merge 1 commit into
base: oss-hackathon-44253
Choose a base branch
from

Conversation

gorosgobe
Copy link

Issue number of the reported bug or feature request: #

Describe your changes
A clear and concise description of the changes you have made.

Testing performed
Describe the testing you have performed to ensure that the bug has been addressed, or that the new feature works as planned.

Additional context
Add any other context about your contribution here.


if (isPropertyAccessExpression(callExpression.expression)) {
const callAccess = callExpression.expression;
if (isIdentifier(callAccess.name) && callAccess.name.escapedText === "hasOwn" && callExpression.arguments.length === 2 && isStringLiteralLike(callExpression.arguments[1])) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A downside to special casing the exact names here is that it would break for cases where the function is assigned to a different name:

const hasProp = Object.hasOwn;

if (hasProp(o, 'test')) ...

I think instead of adding this logic within the compiler itself, we might be able to do it by updating the .d.ts definitions that are packaged with tsc. Something like:

interface ObjectConstructor {
    hasOwn<T, P extends string>(o: T, p: P): o is Extract<T, { [p in P]: unknown }>; 
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it seems like we have the necessary building blocks to express this already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants