-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strict mode static property is undefined (no compiler error) #40649
Comments
This is a interesting one for me, and I tried something similar to this issue, private static props: {
test: string,
test2: number
} = undefined or private static props: {
test: string,
test2: number
} = {} Will raise the expected error. For me this is like let a: number which is actually legal since you might assign to it later. However, private static readonly props: {
test: string,
test2: number
} won't raise the error either, when const a: number is not good. And for instance members, like, private props: {
test: string,
test2: number
}; It always works as expected if they are not assigned in some constructors. I guess there are some difficulties to let static mutable members be checked this way: If non-assigning declaration is disallowed for static mutable members, then non-assigning variable declaration should also be disallowed. However, static read-only members should be forced to be assigned, this is anyway a bug, and For the type check on the private static props?: {
test: string,
test2: number
}; Then |
Yes, the workaround works, but is not perfect. private static f: number; It is declared as a number, but not initialized, so it should be of type "number | undefined" but is of type "number". |
For me I would rather non-assigning non-optional declaration being disallowed. That's to say, the question mark in However this is too much as a breaking change. |
Duplicate of #27899? |
👆 |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 4.0.3
IDE: PHPStorm 2020.2
Search Terms: strictPropertyInitialization static property undefined
tsconfig.json
Code
Expected behavior:
The compiler and IDE should warn me that "props" may be "undefined" and should check their status
Actual behavior:
It will compile and the IDE will not show an error, but the script will crash (StaticUndefined.props is undefined).
Playground link
Related Issues:
The text was updated successfully, but these errors were encountered: