microsoft / microsoft/TypeScript

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

Open
#58,284 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides JavaScript examples using JSDoc typedefs and typed empty object literals; review the compiler's JavaScript type-checking and object-literal assignability behavior first. The payload names no files or tests. Done means an opt-in flag reports missing declared members in these patterns without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.