profile
viewpoint

Ask questionsEmpty object is valid for non exact type annotation

<!-- Please fill out this entire template so that we can address your bug report as quickly as possible.

Any bug reports that don't contain instructions to reproduce the issue will be closed. -->

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

<!-- Please reproduce your issue on flow.org/try so that we can debug it.

Not all issues are reproducible on try-flow because they may require multiple files or specific flowconfig settings. If your bug can only be reproduced under one of these constraints, please make a small github repo that contains a minimal way to reproduce your problem. -->

  • Link to Try-Flow or Github repo: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoAxnAdgZwC5g5wC2ApgPIBGAVgFxgDeYAlgCb1YCuxlpATmAC+YALyNBAbgzZ8YLsQ7deAsUTJVqAOjYSgA
facebook/flow

Answer questions dsainati1

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.

useful!

Related questions

Common spread pattern leads to "exponentially large number of cases" errors hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Object.fromEntries type defs missing hot 1
Cannot set defaultProps with React.memo and React.forwardRef hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
0.92.0 release crashes on startup (Unix.Unix_error) hot 1
module.name_mapper does not find nested modules on windows hot 1
Cannot import useState because there is no useState export in react. hot 1
Can't use a default value from a destructuring assignment as a computed property since 0.111 hot 1
Support optional chaining - flow hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Allow explicitly including a folder under an ignored path hot 1
Flow caches and fails to purge errors hot 1
source:https://uonfu.com/
Github User Rank List