如何在 TypeScript 中创建以枚举作为联合键的 Record<> (或其他映射)

amn*_*sia 4 enums typescript

我需要创建以枚举为键的函数映射,但如果我使用 Record<> 实用程序类型,则似乎需要所有枚举值都存在于映射对象中。我需要它们都是可选的。例如:

enum ContentType {
  Text,
  Image,
  Video,
}
type MapFn = (value: string) => string;

type ContentTypeMap = Record<ContentType, MapFn>;

const myMap = {
  [ContentType.Text] : (value:string) => (value.toUpperCase),
} as ContentTypeMap;
Run Code Online (Sandbox Code Playgroud)

结果是:

Conversion of type '{ 0: (value: string) => () => string; }' to type 'ContentTypeMap' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Type '{ 0: (value: string) => () => string; }' is missing the following properties from type 'ContentTypeMap': 1, 2
Run Code Online (Sandbox Code Playgroud)

我也简单地尝试过:

type ContentMap = { [ContentType]: MapFn };
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨 ContentType 的使用:

A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.
Run Code Online (Sandbox Code Playgroud)

我认为枚举算作文字,但我想在这种情况下不是吗?

(enum) => (function)我可以使用什么来创建对象不需要实现所有枚举值的映射?

Nic*_*wer 5

我也简单地尝试过:

type ContentMap = { [ContentType]: MapFn };
Run Code Online (Sandbox Code Playgroud)

你已经很接近了,但它需要是这样的:

type ContentMap = { [key in ContentType]: MapFn };
Run Code Online (Sandbox Code Playgroud)

然后使它们成为可选:

type ContentMap = { [key in ContentType]?: MapFn };
Run Code Online (Sandbox Code Playgroud)

游乐场链接

如果您更喜欢使用记录表示法,则可以使用实用程序类型Partial使其所有属性可选:

type ContentMap = Partial<Record<ContentType, MapFn>>;
Run Code Online (Sandbox Code Playgroud)

游乐场链接