microsoft / microsoft/TypeScript

`strictNullChecks` short-circuits the subsequent type checks

Open
#53,162 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

hello, I encounter a strange bug when I use strictNullChecks: true, and here is an example:

interface A {
  a: number;
}
const list: A[] = [];
list[0] ??= {};  // throw an error: Property 'a' is missing in type '{}' but required in type 'A'
list[0] = list[0] ?? {};  // the type checking not work !
list[0] = list[0] || {};  // the type checking not work !
🔎 Search Terms
🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

It seems the type checking works after I close the strictNullChecks options
Playground link with relevant code

💻 Code
interface A {
  a: number;
}
const list: A[] = [];
list[0] ??= {};  // throw an error: Property 'a' is missing in type '{}' but required in type 'A'
list[0] = list[0] ?? {};  // the type checking not work !
list[0] = list[0] || {};  // the type checking not work !
🙁 Actual behavior

list[0] = list[0] ?? {}; doens't throw any errors

🙂 Expected behavior

I expect that list[0] = list[0] ?? {}; should throw an error

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

Start with the linked TypeScript Playground and reproduce the example with strictNullChecks enabled, comparing ??= with the subsequent ?? and || assignments. Trace the type-checking path for these assignment forms and add or update a regression test showing that assigning an incomplete object reports the missing property error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.