facebook / facebook/flow

Opting in to getter/setter usage still fails with 'Contravariant property incompatible with covariant use'

Đang mở
#3,191 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
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ả

Flow version 0.37.4.

In the following code:

```
import appendQuery from 'append-query';
import { join } from 'path';
import TypeCastError from './errors/TypeCastError';

export type VersionableArgs = {
instanceId: string,
version: number
};

export default class Versionable {

instanceId: string;
version: number;

constructor(args: VersionableArgs) {
this.instanceId = args.instanceId;
this.version = args.version;
}

set instanceId(val: any) {
if (typeof val !== 'string') throw new TypeCastError('instanceId is expected to be a string');
this.instanceId = val;
}

toUrl(basePath: string) {
let x: string = join(basePath, this.instanceId);
return appendQuery(x, {
version: this.version
});
}

}
```

running `flow check` errors out with:

```
Potentially unsafe get/set usage. Getters and setters with side effects are potentially unsafe and disabled by default. You may opt-in to using them anyway by putting `unsafe.enable_getters_and_setters=true` into the [options] section of your .flowconfig.
```

Opting in to getter and setter usage still fails with:

```
src/Versionable.js:36
36: let x: string = join(basePath, this.instanceId);
^^^^^^^^^^^^^^^ property `instanceId`. Contravariant property `instanceId` incompatible with covariant use in
36: let x: string = join(basePath, this.instanceId);
^^^^^^^^^^^^^^^ property `instanceId`
```

`this.instanceId` is a `string` and `join` takes var args of type `string`… why does this error out?

I am side-stepping the issue with a `// $FlowIssue` comment before `join` usage.

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.