Skip to content

Resolve flow $Keys to union when typeParameter is an ObjectTypeAnnotation #290

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

Merged
merged 6 commits into from Dec 7, 2018
Merged

Resolve flow $Keys to union when typeParameter is an ObjectTypeAnnotation #290

merged 6 commits into from Dec 7, 2018

Conversation

ghost
Copy link

@ghost ghost commented Aug 22, 2018

This adds support for ObjectTypeAnnotations as the typeParameter of $Keys.

This will now resolve to a union:

$Keys<{| a: string, b: string |}>

@ghost
Copy link
Author

ghost commented Aug 27, 2018

Refactored it a bit to use existing util function getPropertyName

name: 'literal',
value: getPropertyName(prop),
})),
};
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we can consolidate this even more since I guess nodes of type ObjectExpression and ObjectTypeAnnotation are very similar. At the very least, can we extract an array of key names for them and process that?

@fkling
Copy link
Member

fkling commented Aug 27, 2018

Thank you for working on this! 😃

@ghost
Copy link
Author

ghost commented Sep 10, 2018

@fkling I updated resolveObjectExpressionToNameArray in utils/resolveObjectKeysToArray.js, so that it handles ObjectExpressions and ObjectTypeAnnotations.

I also noticed that the handleKeysHelper was resolving this to a union:

const obj = { a: 'a', b: 'b' }
const test: $Keys<obj> = 'a'

Which I don't believe is correct. See this example on the flow online repl. I removed this functionality and the associated test.

@ghost
Copy link
Author

ghost commented Oct 8, 2018

Any other changes needed here?

@danez
Copy link
Collaborator

danez commented Oct 30, 2018

I also noticed that the handleKeysHelper was resolving this to a union:

const obj = { a: 'a', b: 'b' }
const test: $Keys<obj> = 'a'

Which I don't believe is correct. See this example on the flow online repl. I removed this functionality and the associated test.

Sorry for the late reply. Thanks for working on this.

I think we should leave the resolving of simple Identifiers in, as for example this could work:

const obj = { a: 'a', b: 'b' }
type A = typeof obj;
const test: $Keys<A> = 'a'

This should resolve correctly to the same result as if doing an inline ObjectExpression, but currently returns unknown. But this is an other problem we can fix separately, but let's leave the resolving of $Keys<A> in for now and it's test. Other than that looks good to me, ready to merge.

@ghost
Copy link
Author

ghost commented Oct 30, 2018

@danez for sure, just pushed those changes.

@danez danez merged commit 471813a into reactjs:master Dec 7, 2018
@danez
Copy link
Collaborator

danez commented Dec 7, 2018

Thank you very much.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants