Currently, I am in the process of experimenting with creating my own basic ORM system. Below is the snippet of code that I have been working on:
type Models = {
User: {
name: string
},
Template: {
text: string
}
}
type ExtractKeysFrom<T> = keyof T
type ModelsKeys = ExtractKeysFrom<Models, Object>
// The expected output here is: 'User' | 'Template'
type ORM = {
model: (modelName: ModelsKeys) => {
create(props: Models[typeof modelName]): Promise<boolean>
}
}
As for the implementation:
declare const orm: () => ORM;
orm().model('User').create({
name: 'Adam'
})
I want the props
parameter in the create()
function to be valid and point only to properties of the User
object. However, currently it shows:
create(props: User | Template): Promise<boolean>
I understand that using typeof modelName
is incorrect in this scenario and it seems like I need to do some type checking with extends
. Could you provide any advice on how to handle this situation properly?