facebook / facebook/flow

Flow error with destructuring assignment when there is an optional and unused parameter

Aperta
#3,908 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
destructuring
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

See demo code below. The type has an optional `status` property.

I'm torn about this one. On the one hand the entire object that is being "destructured" is of the given (whole) type, but on the other hand, destructuring is about getting certain components, so shouldn't it be sufficient to give the type of only those variables that I want to extract? That's what I normally do, only in this case below I have to add type information about items that I don't actually extract. And it's only half necessary, I don't need to give a type for the `type` property, but I have to specify IN FULL the inner object for some reason.

However, the destructuring assignment doesn't use it, but I have to include it in the Flow type spec for the inner `map` function in this demo:

```javascript
// @flow
'use strict';

type ReferenceObj = {|
type: 'Reference',
data: {
// This optional property causes the error
status?: string,
reference: string,
type: string,
id: string
}
|};

const demo1 = function (personReferences: Array): void {
personReferences.map(
({
data: {
reference,
type,
id
}
}: {
data: {
//status?: any, // Flow issue - UNUSED
reference: string,
type: string,
id: string
}
}) => {
// ....
}
);
};

// @formatter:off
/*

This happens only with the optional "status?" property in ReferenceObj:

$ flow check
16: personReferences.map(
^ call of method `map`
4: type ReferenceObj = {|
^ object type. This type is incompatible with the expected param type of
23: }: {
^ object type
Property `data` is incompatible:
6: data: {
^ property `status`. Property not found in
24: data: {
^ object type

Found 1 error
*/
```

There is no problem when either

- I remove the `status?` property from the `ReferenceObj` type, or
- I put in the useless and unused `status?` type in the destructuring type.

What also works is `({data: {reference, type, id}}: ReferenceObj) => ...`

Flowconfig:

```
[include]
./src/

[options]
module.ignore_non_literal_requires=true
```

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.