typescript - 将“as const”对象转换为更通用的类型
问题描述
as const
从const 对象派生通用类型/接口
我有这个:
const usersDefaultValues = {
firstName: '',
isGuest: false
} as const
我想派生这种类型:
type DefaultValuesSchema = {
firstName: string
isGuest: boolean
}
解决方案
我认为您正在寻找这样的东西:
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>
推荐阅读
- azure - How to set up Azure IP alias directly using DNS name?
- python - 抓取 Java 网页
- excel - Using a MsgBox and If Then to open another worksheet
- javascript - 如何在这两个 mongoose.js 1-1 关系上添加一个 id
- c++ - 如何通过另一个函数传递不断变化的成员函数?
- node.js - 如何在 Express.js 的 URL 中传递字符串列表
- javascript - 使用 Node.js 的 https.request() 并输出到浏览器
- javascript - Reactjs 悬停并显示问题
- android - 将参数传递给 Kotlin 中的函数
- r - 使用一列的一部分在 R 中对数据集进行子集