microsoft / microsoft/TypeScript
Dynamic Super Types
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Now, that #11929 is PR landed. I would really love to have some syntax to specify an arbitrary super type of T. This can be especially helpful in creating strongly typed ORM database frameworks.
Proposal
If K is constrained to be a key of T i.e K extends keyof T. Then the syntax:
{ ...K: ...T[K] }
Means spread the keys K and values of T in {}.
Example
Here is an example usage of an envisioned ORM framework:
interface IUser {
id: string;
name: string;
email: string;
createdAt: string;
updatedAt: string;
password: string;
// ...
}
interface Options<T, K extends keyof T> {
attributes: T[K][];
}
interface Model<IInstance> {
findOne<K extends keyof IInstance>(options: Options<IInstance, K>): { ...K: ...IInstance[K] };
}
declare namespace DbContext {
define<T>(): Model<T>;
}
const Users = DbContext.define<IUser>({
id: { type: DbContext.STRING(50), allowNull: false },
// ...
});
const user = Users.findOne({
attributes: ['id', 'email', 'name'],
where: {
id: 1,
}
});
user.id // no error
user.email // no error
user.name // no error
user.password // error
user.createdAt // error
user.updatedAt // error
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 proposed { ...K: ...T[K] } syntax and the ORM example to understand the intended result type. The issue names no implementation files, entry points, or tests, so research the type-system and parser areas before defining the semantics and adding coverage for the demonstrated selected-attribute behavior.
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
- Mostly clear
- Newbie friendliness
- 25/100