microsoft / microsoft/TypeScript
Distributive as const
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Search Terms
as const, widening, literal widening
Suggestion
Allow to use as const on ternary operator as well as others code branches that currently we can't.
Use Cases
Let's say that we have this code:
const result = (
value > 5
? { amount: 'many' } as const
: { amount: 'few' } as const
)
We need to type as const twice. So would be nice if we could type just once:
const result = (
value > 5
? { amount: 'many' }
: { amount: 'few' }
) as const
Currently we can't do that (playground).
It's just a syntax sugar. It won't change anything on as const!
Analogously, maybe we could allow this syntax sugar on function:
const func = (param: number) => {
if (param > 5) {
return 'many' as const
}
return 'few' as const
}
// would be just...
const func = (param: number): const => {
if (param > 5) {
return 'many'
}
return 'few'
}
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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 dall’esempio collegato di TypeScript Playground per riprodurre il posizionamento rifiutato di as const e confrontarlo con la forma accettata a livello di ramo. Non sono indicati file sorgente o test; individua i punti di ingresso del compilatore e dei test per le const assertions, quindi verifica che la sintassi ternaria proposta venga accettata senza modificare il JavaScript emesso né il comportamento esistente.
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