microsoft / microsoft/TypeScript
Allow to overwrite generic templates
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los ejemplos de generic-template del issue y revisa cómo TypeScript modela actualmente las clases genéricas y los tipos de retorno de los métodos. Determina la semántica prevista de la comprobación de tipos y evalúa el diseño requerido del compilador antes de identificar las ubicaciones de implementación y de las pruebas; se considera terminado cuando los casos propuestos pasan la comprobación de tipos sin cambiar el JavaScript emitido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100