microsoft / microsoft/TypeScript
Declare generics in type assertion / generic constraint and reuse
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
generic, type assertion, narrowing, constraint, reuse
Suggestion
type Fn<T> = (subject: T) => void
const boolFn = fn as (<T extends boolean>Fn<T>)
// or a less flexible, but easier approach
const boolFn = fn as Fn<T extends boolean>
Today there is no way to do that.
The following does not work:
declare type boolFn<T extends boolean> = Fn<T>
const boolFn = fn
The current workaround is to redeclare or compose the base function:
const boolFn = function <T extends boolean>(subject: T) { return fn(subject) }
This unnecessary increase the size of the code.
Use Cases
There are cases where the functionality of a function only differs in their types.
To provide the best experience in TypeScript,
I would like to create some of the common variations of these function but using the same underlying function to keep the actual code size small.
Here is a simple example in type-plus:
export namespace assertType {
export type Fn<T> = (subject: T) => void
}
export function assertType<T>(subject: T) { return }
assertType.isUndefiend = assertType as assertType.Fn<undefined> // ok
assertType.isBoolean = assertType as (<T extends boolean = boolean>assertType.Fn<T>) // propose
// usage
let subject: boolean | string = ...
assertType.isBoolean<false>(subject)
This proposal allows better reuse of generic types and provide better composability.
It also narrow the gap between the ability to type functions vs variables.
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
La proposta non indica alcun file sorgente, test o punto di ingresso dell’implementazione. Inizia individuando la gestione del compilatore per type assertions, generic constraints e callable signatures; il lavoro è completato quando gli esempi proposti superano il type-check senza modificare il JavaScript emesso.
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