| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
Add check for abstract properties |
Sorry, something went wrong.
Is there a reason TS can't check if the called functions access the uninitialized value, then report the error in the constructor? |
Sorry, something went wrong.
|
@kingdaro we only do control flow analysis across function bounds for immediately invoked function expressions; to do otherwise would be error-prone in the presence of aliasing, so we conservatively avoid doing so. |
Sorry, something went wrong.
|
What about initializing helpers? |
Sorry, something went wrong.
|
abramobagnara I'd propose one of:
|
Sorry, something went wrong.
|
I have edited the PR description to reflect #20166. |
Sorry, something went wrong.
Will be really useful to have a warning in such case. |
Sorry, something went wrong.
|
Also, does this PR handle cases where error is thrown before all fields initialised? class Car {
color: string;
constructor() {
if (!carsSupportedByBrowser) {
console.log('Cars not supported');
throw new Error('Not supported');
}
this.color = 'red';
}
} |
Sorry, something went wrong.
|
And a quirkier variation of previous case: class Car {
color: string;
constructor() {
if (!carsSupportedByBrowser) {
console.log('Cars not supported ', this); <--- see we expose instance with uninitialised color
throw new Error('Not supported');
}
this.color = 'red';
}
} |
Sorry, something went wrong.
It is fine to throw an exception before all properties are initialized, and yes you can expose an object that isn't fully initialized to the outside (just like you can call methods before the object is fully initialized). |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
This PR implements a new --strictPropertyInitialization compiler option to guard against uninitialized properties in class instances. Strict property initialization checking verifies that each instance property declared in a class either (a) has a type that includes undefined or (b) has an explicit initializer or an assignment to the property in the constructor. The --strictPropertyInitialization option has no effect unless --strictNullChecks is also enabled.
The --strictPropertyInitialization switch is part of the --strict family of switches, meaning that it defaults to on in --strict mode. This PR is therefore a breaking change only in --strict mode.
In this example
an error is reported on property a because its type doesn't include undefined and the property declaration doesn't specify an initializer or assign a value to the property in the constructor. The error can be fixed by changing the type of a to include undefined, by specifying an initial value for a, or by assigning a value to a in the constructor. For example, this is ok:
A strict property initialization check is satisfied by assignments in the constructor only when all possible code paths include assignments to the particular property.
In the example above, an error would occur if either of the if statement branches omitted the assignment to this.a.
Strict property initialization checks guard against observing uninitialized properties in the constructor body. For example:
Strict property initialization checks also guard against observing uninitialized properties after the constructor returns. However, it is possible to observe uninitialized properties in methods that are called from the constructor (or from a base constructor). For example:
Strict property initialization checks only apply to properties that are declared with proper identifiers. It does not apply to properties with computed names or properties with names specified as string or numeric literals (this is analogous to how control flow analysis isn't applied to properties accessed using obj["foo"] syntax).
In cases where the properties of a class instance are initialized by external means (such as dependency injection) or where it is known that an initialization method is always called before other methods, strict property initialization checks can be suppressed by including definite assignment assertions (#20166) in the declarations of the properties.
Alternatively, // @ts-ignore comments can be used to suppress the errors, or the code can simply be compiled without the --strictPropertyInitialization option.
Fixes #8476.