Add a flag to require JS object literals to be initialized with all declared members #58284
Open
6 tasks done
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
I'm aware JavaScript files work somewhat differently from TypeScript files, so I'm assuming this behavior is intentional. But if not, feel free to consider this as a bug instead.
π Search Terms
empty object label:"Domain: JavaScript"
β Viability Checklist
β Suggestion
In JavaScript files it is possible to assign an empty object to a variable, as long as you provide it with the expected properties later:
But this can cause all sorts of issues later down the line. In .ts files this is not an issue because this behavior immediately emits an error when assigning the empty object.
It would be nice to have a new compiler option that makes this behaviour more strict, similar to the current behavior in .ts files.
π Motivating Example
Consider the following code:
TypeScript acts as if there is nothing wrong with this, but when running this, you'll end up with an error when trying to use
x.myCoolString
before it's been assigned.Perhaps an even more subtle situation would be where you forget to assign one of your properties:
In this case, it's easy to imagine a scenario where you add a new
@property
to the@typedef
comment, expecting TypeScript to warn you about all cases in your codebase where you may be creating new objects. But in reality, if you are using dot notation anywhere in your codebase to createFoo
, then you'll be out of luck.π» Use Cases
Stricter type checking!
The text was updated successfully, but these errors were encountered: