microsoft / microsoft/TypeScript
Suggestion: Reference expression type during cast
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Search Terms
cast reference expression type
Suggestion
I would like to be able to reference original type of the expression during cast.
For example:
const o = {
x: { a: 1 } as typeof & { b?: number }
}
// type of o.x is now { a: number; b?: number }
I am not attached to typeof syntax here, can be anything that makes sense.
Use Cases
- Defining optional properties without actually initializing them.
Current approach would require me to do something like:
const o = {
x: { a: 1 } as { a: number; b?: number }
}
which is unweildy and repeats all the properties.
It is possible to do through a function, but it has an overhead and is complicated due to partial generics:
function extend<T>(T data) {
return <TExtra>() => data as T & TExtra;
}
const o = {
x: extend({ a: 1 })<{ b?: number }>()
}
- Adding indexers to object literals:
function map1(key: string) {
return { a: 1, b: 2 }[key] || 3; // does not work due to https://github.com/microsoft/TypeScript/issues/14951
}
function map2(key: string) {
return ({ a: 1, b: 2 } as { [key: string]: typeof[keyof typeof]|undefined })[key] || 3;
}
Examples
See use cases.
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
Nell'issue non sono indicati file sorgente, test o punti di ingresso. Inizia esaminando la sintassi di cast proposta e entrambi i casi d'uso, quindi determina il design necessario per il sistema dei tipi e la sintassi; il lavoro sarebbe completo con un approccio accettato e il relativo ambito di implementazione.
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
- Da chiarire
- Idoneità per principianti
- 30/100