microsoft / microsoft/TypeScript
Allow to overwrite generic templates
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generic-template examples in the issue and review how TypeScript currently models generic class and method return types. Determine the intended type-checking semantics and assess the required compiler design before identifying implementation and test locations; done means the proposed cases type-check without changing emitted JavaScript.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100