将"as const"对象转换为更通用的类型



as const常量对象派生广义类型/接口

我有这个:

const usersDefaultValues = {
firstName: '',
isGuest: false
} as const

我想推导出这种类型:

type DefaultValuesSchema = {
firstName: string
isGuest: boolean
}

我想你正在寻找这样的smth:

const usersDefaultValues = {
firstName: '',
isGuest: false
} as const
type M<T> = {
[P in keyof T]: T[P] extends boolean ? boolean : T[P] extends string ? string : never;
}
type Result = M<typeof usersDefaultValues>

相关内容

最新更新