Devin Rhode
Devin Rhode

Reputation: 25347

Create typescript interface based on a const defaultValues map

I wanted to do some something similar to this: https://stackoverflow.com/a/45777530/565877

But I'm not working with any sort of real database schema, I just have a simple object of field names and their default values, like so:

export const FormFieldDefaults = {
  firstName: '',
  lastName: '',
  dateOfBirth: ''
}

I want to generate this respective type:

export type FormFields = {
  firstName: string
  lastName: string
  dateOfBirth: string
}

Upvotes: 1

Views: 204

Answers (1)

Devin Rhode
Devin Rhode

Reputation: 25347

Turns out all we need is the typeof type operator:

export const FormFieldDefaults = {
  firstName: '',
  lastName: '',
  dateOfBirth: ''
}

export type FormFields = typeof FormFieldDefaults

Upvotes: 1

Related Questions