【发布时间】:2021-11-29 08:36:32
【问题描述】:
我需要创建一个 Typescript Record,其中的键定义为独立类型和每个值的特定类型。
键定义如下:
// Keys must be available/iterable at runtime
const keys = ['a', 'b', 'c'] as const
export type Key = typeof keys[number]
现在我看到了两个选项,都是有缺陷的。
选项 1:根据需要重复键并明确定义值类型。缺陷:Structure 实际上并不是基于Key,它们可能会出现偏差。
export type Structure1 = {
a: number
b: boolean
c: string
}
选项 2:从 Key 定义记录并丢失值的特定类型信息:
export type Structure2 = Record<Key, number | boolean | string>
对于使用Key 作为键类型和每个键的显式值类型的Structure3,是否有第三种选择?
【问题讨论】:
-
为什么第二个选项不好?
-
因为
Structure2将接受boolean或string为a -
您是否正在寻找将值类型定义为元组 (
[number, boolean, string]) 并按顺序使用键“压缩”? -
在这种情况下你需要有一些地图类型
-
然后你可以定义一个对象并从中提取类型和键typescriptlang.org/play?#code/…
标签: typescript record