Get fields as type #416
Answered
by
noam-honig
Darkside73
asked this question in
Q&A
-
Is it possible to get entity's fields as type? For example: @Entity('posts')
export class Post {
@Fields.cuid()
id = ''
@Fields.string()
title = ''
}
type PostFields = ExtractFields<Post> Produces: type PostFields = {
id: string
title: string
} |
Beta Was this translation helpful? Give feedback.
Answered by
noam-honig
Apr 23, 2024
Replies: 1 comment
-
You can achieve what you want with: export class Post {
id = ''
title = ''
}
type PostFields = InstanceType<typeof Post>; But that's actually redundant, since in typescript a class is also a type - so the type so: let x: Post = {
id: '1',
title: 'noam'
}
//is identical to:
let y: PostFields = {
id: '1',
title: 'noam'
} Checkout this typescript playground |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Darkside73
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can achieve what you want with:
But that's actually redundant, since in typescript a class is also a type - so the type
Post
has these exact same definition as thePostFields
type created here.so:
Checkout this typescript playground
https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwDYEMDOa4AUJvgbwCg44BLAEzgF44ByW4uGUmJYauhgX0JgE8w7HHgBipYEnKYaASQB2eFHITAAKgOAAefoIgAzbLhgA+ANyFCbeCABchvByIkKd2gEZaAGiYs2ruRAoALbclsDwfHbCMGISUo6MLnQenozMrMD+gSGEPEA