Repository: TypeScript
Issue: Refine type checking for class accessor when using decorator 
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

decorator, class accessor, auto-accessor, types, null checks.

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->

If a class accessor decorator will always add an initial value for the decorated accessor, TypeScript should infer that the accessor is always non-null, even if no initial value in its original declaration.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->

See [playground](https://www.typescriptlang.org/play?experimentalDecorators=false&emitDecoratorMetadata=false&ts=5.0.0-dev.20230201#code/GYVwdgxgLglg9mABAEwKYQDwBUAWMDOANIgGoCGANiKgHwAUAUIs4lGQE4DmqUAXIgGEKZfPgCCECKlFx2AEXSyyUWVg7co2PEUQB1dmQAOh1OwzkqtGoSYsICKKgAefQcNESpM+YoMr2Ag7OmrgExPpGJmYW1DTWDACU-EIi4pLS+LIK9n6yAErSIBQh2uEGxqbmlLE0iADetszsPCDsSA0snYgwYDBQdD19MJQxqAn1jV2dMMCIA72wI9WoiACEALzriOBowD2oyOMdUydNLW3dC8MUowDck6cAvg8nzVCt7Y-3T5PPzww9RzsYBkKR6cpRbCILbgADWYDgAHcwLVjswAG7LAD8-Cw93+EHc+EQWGkUAmnQAAmgIIhQV5MuwUMoyPwIhUzPgoOwepwaPiGEA).

The decorator `dec` will always initialize the accessor with a non-null value, but TS reports an error on `@dec accessor data: Wrapper<string>;`. It says "Property 'data' has no initializer and is not definitely assigned in the constructor" when `strictNullChecks` is enabled.

```ts
function dec<This, Value>(
    target: ClassAccessorDecoratorTarget<This, Wrapper<Value>>,
    context: ClassAccessorDecoratorContext<This, Wrapper<Value>>,
): ClassAccessorDecoratorResult<This, Wrapper<Value>> {
    return {
        init(initialValue) {
            if (initialValue !== undefined) {
                return initialValue;
            }
            return {};
        }
    }
}

interface Wrapper<T = unknown> {
    value?: T;
}

class Test {
    @dec accessor data: Wrapper<string>;
}
```

<img width="818" alt="image" src="https://user-images.githubusercontent.com/2338946/216225114-a205823c-6b60-4d26-91df-edd048553cec.png">

Are there any workarounds, other than putting an `!` after `data`, to make the type checking works?

## 💻 Use Cases

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->