microsoft / microsoft/TypeScript
Allow to overwrite generic templates
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
- overwrite generic this type
- overwrite template argument
- class overwrite generic
- overwrite type generic
Suggestion
Allow to overwrite generic type templates.
Use Cases
I'm currently developing a modular ORM and query builder, where especially in the query builder part it would be necessary to overwrite a generic template argument.
Examples
Here's a super simple example of a query builder. The select method should return a new instance of Query but with a different subset of T (since it limits the fields being selected).
interface Entity {}
class Query<T extends Entity> {
findOne(): T;
select(names: (keyof T)[]): this<Partial<T>>; //doesn't work
}
interface User {
username: string;
group: string;
created: Date;
}
const item: User = new Query<User>().findOne();
const item: Partial<User> = new Query<User>().select(['username']).findOne();
This is just to demonstrate the use case. You could of course further describe the type of select by using Pick etc, but I wanted to keep it simple. Please note that I could return Query<Partial<T>> instead, but that would break class inheritance.
To my awareness this use case can't be covered with the newest TypeScript version.
Another use case I had:
interface Query<T> {
findOne(): T;
}
interface AdapterInterface {
createQuery<T extends Entity>(classType: Constructor<T>): Query<T>;
}
class Database<A extends AdapterInterface> {
constructor (private adapter: A) {}
// here is the magic with the return type, which doesn't work, but is necessary since
// `T` of A['createQuery'] needs to be inferred from query<T>.
query<T extends Entity>(classType: Constructor<T>): ReturnType<A['createQuery']><T> {
return this.adapter.createQuery(classType);
}
}
class MyAdapterQuery<T> extends Query<T> {
additionalMethod(): this;
}
class MyAdapter {
createQuery<T extends Entity>(classType: Constructor<T>): MyAdapterQuery<T> {
return new MyAdapterQuery<T>();
}
}
const database = new Database(new MyAdapter);
database.query(User).additionalMethod();
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 con gli esempi di generic-template nell’issue e verifica come TypeScript modella attualmente le classi generiche e i tipi restituiti dai metodi. Determina la semantica prevista del controllo dei tipi e valuta la progettazione necessaria del compilatore prima di individuare le posizioni per l’implementazione e i test; il lavoro è completato quando i casi proposti superano il controllo dei tipi 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
- Da chiarire
- Idoneità per principianti
- 25/100