microsoft / microsoft/TypeScript
Refine type checking for class accessor when using decorator
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Suggestion
🔍 Search Terms
decorator, class accessor, auto-accessor, types, null checks.
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
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
See playground.
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.
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>;
}
Are there any workarounds, other than putting an ! after data, to make the type checking works?
💻 Use Cases
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio collegato di TypeScript Playground e riproduci la diagnostica con strictNullChecks per l’accessor decorato. Leggi il comportamento del decorator e dell’accessor di classe descritto nell’issue, quindi determina la modifica necessaria al controllo dei tipi affinché l’accessor venga trattato come inizializzato; il lavoro è completato quando l’esempio non richiede più una non-null assertion e il comportamento esistente a runtime viene preservato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100