facebook / facebook/flow

Improve error message for undeclared class property initialization

Open
#643 0 comments 2 reactions 0 assignees View on GitHub
error messages
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Flow requires that class properties be declared before they can be initialized in a constructor. This behavior is reasonable, but the error message should guide the user to the solution:

``` javascript
/* @flow */

class Foo {
constructor() {
this.bar = "baz";
}
}
```

```
test.js|5 col 5 error| assignment of property bar
|| Property not found in
test.js|3 col 7 error| Foo
||
```

A better error message would direct the user to declare the type of `bar`, as follows:

``` javascript
/* @flow */

class Foo {
bar: string;
constructor() {
this.bar = "baz";
}
}
```

```
|| No errors!
```

A better message might be "Property must be declared in class."

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.