facebook / facebook/flow

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

Đang mở
#3,908 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
destructuring
Ngôn ngữ chính
Rust
Star
22.3k
Fork
1.9k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.