基于const defaultValues映射创建typescript接口



我想做一些类似的事情:https://stackoverflow.com/a/45777530/565877

但我不使用任何类型的真实数据库模式,我只是有一个字段名称及其默认值的简单对象,如下所示:

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

我想生成相应的类型:

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

我们只需要typeof类型的运算符:

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

最新更新