Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Support for Object.hasOwn narrowing#71

Draft
gorosgobe wants to merge 1 commit into
bloomberg:oss-hackathon-44253bloomberg/TypeScript:oss-hackathon-44253from
gorosgobe:object-has-owngorosgobe/TypeScript:object-has-ownCopy head branch name to clipboard
Draft

Support for Object.hasOwn narrowing#71
gorosgobe wants to merge 1 commit into
bloomberg:oss-hackathon-44253bloomberg/TypeScript:oss-hackathon-44253from
gorosgobe:object-has-owngorosgobe/TypeScript:object-has-ownCopy head branch name to clipboard

Conversation

@gorosgobe

Copy link
Copy Markdown

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.

Comment thread src/compiler/checker.ts

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

Copy link
Copy Markdown

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 }>; 
}

Copy link
Copy Markdown

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.