facebook / facebook/flow

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

Offen
#3,908 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
destructuring
Vorherrschende Sprache
Rust
Sterne
22.3k
Forks
1.9k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.