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

Empty object is valid for non exact type annotation #8430

Closed
diverpb opened this issue Jul 4, 2020 · 8 comments
Closed

Empty object is valid for non exact type annotation #8430

diverpb opened this issue Jul 4, 2020 · 8 comments

Comments

@diverpb
Copy link

diverpb commented Jul 4, 2020

Flow version: 0.128.0

Expected behavior

Type error

Actual behavior

const someObj: { id: number } = {};
const num: number = someObj.id;
console.log(num);

num here is undefined, and definitely not a number

@jcready
Copy link
Contributor

jcready commented Jul 5, 2020

This is just how unsealed objects work in Flow.

@levenleven
Copy link

levenleven commented Jul 6, 2020

@jcready How is this related to sealed/unsealed if the type is explicitly specified? Also if it would be unsealed the following would be allowed:

const someObj: { id: number } = {};
someObj.foo = 'foo';

but it is not https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoAxnAdgZwC5g5wC2ApgPIBGAVgFxgDeYAlgCb1YCuxlpATmAC+YALyNBAblREyVagDoocOKLAByJXDUSgA

@dsainati1
Copy link
Contributor

Annotating the type of someObj limits what you can do with it. When initializing someObj, you write an unsealed object to the variable, but then tell Flow you only want to allow it to be used as if it is a value of type {id : number}. So the unsealedness of the empty object literal is masked by the fact that you are immediately annotating the value you are assigning it to.

@levenleven
Copy link

Thanks @dsainati1 this makes it clearer. (I still feel like it is not desired behavior)

@dsainati1
Copy link
Contributor

Agreed. We have wanted to get rid of unsealed objects for a while, the difficulty is devising a new way to support the existing pattern of building up an object over time.

@achepukov
Copy link

@jcready
Copy link
Contributor

jcready commented Jul 8, 2020

See #7424

@gkz
Copy link
Member

gkz commented Jul 13, 2022

Fixed if you enabled exact_empty_objects=true in your .flowconfig.
Announcement will be in the future

@gkz gkz closed this as completed Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants