Skip to content
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

Add a flag to require JS object literals to be initialized with all declared members #58284

Open
6 tasks done
jespertheend opened this issue Apr 22, 2024 · 4 comments
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

Comments

@jespertheend
Copy link
Contributor

jespertheend commented Apr 22, 2024

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:

/**
 * @typedef Foo
 * @property {string} hello
 */

/** @type {Foo} */
const x = {};
x.hello = "hello";

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:

/**
 * @typedef Foo
 * @property {string} myCoolString
 */

/** @type {Foo} */
let x = {};

x.myCoolString.split(","); // Cannot read properties of undefined!

x.myCoolString = "hello";

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:

/**
 * @typedef Foo
 * @property {string} str1
 * @property {string} str2
 */

/** @type {Foo} */
let x = {};
x.str1 = "hello";

x.str2.split(","); // Cannot read properties of undefined!

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 create Foo, then you'll be out of luck.

πŸ’» Use Cases

Stricter type checking!

@jcalz

This comment was marked as resolved.

@RyanCavanaugh RyanCavanaugh changed the title Add a flag to disallow Add a flag to require JS object literals to be initialized with all declared members Apr 22, 2024
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Apr 22, 2024
@fatcerberus
Copy link

@jcalz Hit-Enter-Too-Soon Man strikes again!

@jcalz

This comment was marked as off-topic.

@jespertheend
Copy link
Contributor Author

I could swear I typed the full title haha. Sorry about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

4 participants