Skip to content

Get fields as type #416

Answered by noam-honig
Darkside73 asked this question in Q&A
Discussion options

You must be logged in to vote

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 Post has these exact same definition as the PostFields type created here.

so:

let x: Post = {
   id: '1',
   title: 'noam'
}
//is identical to:
let y: PostFields = {
  id: '1',
  title: 'noam'
}

Checkout this typescript playground
https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwDYEMDOa4AUJvgbwCg44BLAEzgF44ByW4uGUmJYauhgX0JgE8w7HHgBipYEnKYaASQB2eFHITAAKgOAAefoIgAzbLhgA+ANyFCbeCABchvByIkKd2gEZaAGiYs2ruRAoALbclsDwfHbCMGISUo6MLnQenozMrMD+gSGEPEA

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Darkside73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants