Flow: Renaming when destructuring objects

Created on 6 Apr 2021  路  2Comments  路  Source: facebook/flow

Flow version: v0.148.0

The following code

const { propName: newPropName }: {| newPropName: string |} = { propName: "..." };

cause the following errors:

  • property propName is missing in object type [1]. [prop-missing]
  • Cannot assign object literal to destructuring because property newPropName is missing in object literal [1] but exists in object type [2]. [prop-missing]
  • Cannot assign object literal to destructuring because property propName is missing in object type [1] but exists in object literal [2]. [prop-missing]

Of course I could do:

const { propName }: {| propName: string |} = { propName: "..." };
const newPropName: string = propName;

which is perfectly fine with Flow but I would love to benefit the very convenient syntaxe above 馃檪
Maybe this is more like a feature request than a bug. I let you choose 馃憤

https://flow.org/try/#0MYewdgzgLgBA3jADgJxIgcgQwLYFMBcMYuA7gAqoY64wC+hcAPkaRWlnodMgJZgDmMRrRgBeeEkocCMAEQA6RbLoBuAFBqA9JpihIsBCnbU6DZkaqcY3PoOFiJF6YQVLVWnXugtyU6lyheAQcnahUgA

bug needs triage

Most helpful comment

I think your syntax is simply wrong. Your object property def should be propName not newPropName as shown below. Because you are not creating a new object with a property of newPropName, you are destructuring the current object, and taking the value propName and then reassigning the variable name after the fact.

If you in fact check the value that flow infers and assigns to newPropName it will be string, as you have typed into propName.

const { propName: newPropName }: {| propName: string |} = { propName: "..." };

https://flow.org/try/#0PTAEGMHsDsGcBdQG9QAcBOlUDkCGBbAUwC5RpCB3ABUxwMNAF9SkAfMymrPI0hdAJbQA5qFaNQAXmRpaPEqABEAOlWKmAbgBQWkBBgIZGbvSYt2xur1D8ho8VKNz6pFWs26wUOInLVn1rYijpby2lrehiihLhz+JkRmyBYBCkH2EtLRqa6qyuqMGkA

All 2 comments

I think your syntax is simply wrong. Your object property def should be propName not newPropName as shown below. Because you are not creating a new object with a property of newPropName, you are destructuring the current object, and taking the value propName and then reassigning the variable name after the fact.

If you in fact check the value that flow infers and assigns to newPropName it will be string, as you have typed into propName.

const { propName: newPropName }: {| propName: string |} = { propName: "..." };

https://flow.org/try/#0PTAEGMHsDsGcBdQG9QAcBOlUDkCGBbAUwC5RpCB3ABUxwMNAF9SkAfMymrPI0hdAJbQA5qFaNQAXmRpaPEqABEAOlWKmAbgBQWkBBgIZGbvSYt2xur1D8ho8VKNz6pFWs26wUOInLVn1rYijpby2lrehiihLhz+JkRmyBYBCkH2EtLRqa6qyuqMGkA

Oh, ok I get it, sorry for the confusion. That makes sense.

As the "renamed property" inherit the type it's perfectly fine 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamiebuilds picture jamiebuilds  路  3Comments

Beingbook picture Beingbook  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments

damncabbage picture damncabbage  路  3Comments

john-gold picture john-gold  路  3Comments