microsoft / microsoft/TypeScript
Allow non-readonly properties to be used in aliased conditional expressions
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Suggestion
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.
🔍 Search Terms
aliased conditional expressions condition control flow readonly property
⭐ Suggestion
Some beta feedback. I was excited to try out TypeScript 4.4 because it provided control flow analysis of aliased conditions but was disappointed to learn that it only works for readonly properties as mentioned in the implementation PR. I get the justification; unfortunately, I don’t use readonly because I don’t think the weak immutability guarantees which the modifier provides are worth the syntax noise or annoying assignability errors.
📃 Motivating Example
I digress. Limiting control flow analysis to readonly properties makes code like the following not work, which I would argue is somewhat surprising.
interface A {
prop: string | undefined;
}
declare const a: A;
// this works
if (a.prop !== undefined) {
const b: string = a.prop;
console.log(b);
}
const hasProp = a.prop !== undefined;
// this errors
if (hasProp) {
const b: string = a.prop;
console.log(b);
}
💻 Use Cases
I just want to write code which can take advantage of aliased property checks without having to use the TypeScript’s readonly modifier.
Maybe we could assume some basic things about JavaScript object properties for the sake of developer experience. For instance, we could assume that non-getter property test aliases are safe to use in control flow analysis so long as the property is not directly reassigned.
In any case, I think adding this caveat to blog posts and all marketing copy describing the feature would be wise, because I will likely not be the last person to be confused by this limitation, and many TypeScript features are only documented in release notes.
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
Non viene indicato alcun file di implementazione, test o punto di ingresso. Inizia dall’esempio TypeScript motivante e dalla discussione collegata sull’implementazione dell’analisi del flusso di controllo, quindi determina le regole sicure per gli alias di proprietà non readonly; il lavoro è completato quando l’esempio viene ristretto correttamente senza modificare il JavaScript emesso e la limitazione è documentata dove appropriato.
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
- 30/100