Repository: TypeScript
Issue: Using nullish coalescing with untyped field in constructor trigger TS7022
Labels: Help Wanted,Possible Improvement

# Bug Report

Using nullish coalescing in a constructor to initialize a class member with no type annotation will result in a TS7022 error.

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

`nullish`, `coalescing`, `7022`, `??`, and `ts7022`

<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->

### 🕗 Version & Regression Information

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing and type inference.

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior.
  This should have the same code as the code snippet below, and use whichever settings are relevant to your report.

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?ts=5.1.6#code/KYDwDg9gTgLgBAYwDYEMDOa4DkIBNhwDeAUHHAMRgpTAB2MxpFCAFgJZK421wC8ctYAHc4AYXaduAChjs0ASkZkEEWmhhQArghjQ4UqtxgB+AFzY8BAD4DNSJHBuba+AGZtBueUSZkA9H5wAErAALYQAG4EAAbGxtFwKC5wsgQq+ImuMMBQicmpiJaFoWAcwLgAdBYilNR08GyYzm4e5XBC7KxwjQIQ8PhobDSVvilyFbVGfHCG9XBxtvZMAL5KcADmwPCz9PrehHA0MJpQPLKNEzvwq0yb8KwcXHR7RIdbJ2fj5A+Sz6s3oEgsEQqAwYgkTx4JDIk3qTHILGAKFw8JgKA4axUag02l0uQMdXo5hw+H2o3OaEuhPg-Cu5K+iOR00EIhJwAJRkUZDIFImaI4zOEFnwHPqXLg-2IQA)

### 💻 Code

<!-- Please post the relevant code sample here as well. This code and the Playground code should be the same, do not use separate examples -->
```ts
export class Node {
  #parent

  #children = new Children(this)

  constructor (parent?: Node | null | undefined) {
    // Remove `??` and the code after and the code compiled. Now #parent is undefined which is not desired.
    this.#parent = parent ?? null
  }

  get parent () { return this.#parent }

  get children () { return this.#children }
}

export class Children {
  #parent
  #head
  #tail

  constructor (parent: Node) {
    this.#parent = parent
    this.#head = new Node(parent)
    this.#tail = new Node(parent)
  }
}
```

### 🙁 Actual behavior

<!-- What happened, and why it was wrong -->
Attempting to compile the example code results in a TS7022 error: `'#parent' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.`

### 🙂 Expected behavior

<!-- What you expected to happen instead, and why -->
The code should compile and `#parent` should be typed as `Node` | `null`.