如何将 `readonly string[]` 变成 `string[]`?

How to get a `readonly string[]` into a `string[]`?

我有以下数据设置(未声明为 enum,因为它在 TypeScript 服务器代码和非 TypeScript 客户端代码之间共享):

import { enumType } from 'nexus';

export const TYPE_ENUM = Object.freeze({
  H: 'H',
  S: 'S',
});
export const TYPES = Object.freeze(Object.values(TYPE_ENUM));

export const MyType = enumType({
  name: 'MyType',
  members: TYPES,
});

TypeScript 给我以下关于 members 字段的警告:

Type '(readonly string[])[]' is not assignable to type '(string | EnumMemberInfo)[] | Record<string, string | number | boolean | object>'.
  Type '(readonly string[])[]' is not assignable to type '(string | EnumMemberInfo)[]'.
    Type 'readonly string[]' is not assignable to type 'string | EnumMemberInfo'.
      Type 'readonly string[]' is not assignable to type 'string'.ts(2322)
enumType.d.ts(29, 3): The expected type comes from property 'members' which is declared here on type 'EnumTypeConfig<"MyType">'

我理解错误。不能将 readonly string[] 放入期望 string[] 的东西中,因为它们是不同的类型。我的问题是,克服这个问题的最佳方法是什么?

我已经看到展开并重新创建数组是有效的:members: [...TYPES] 但感觉不对。

您已经有了答案:members: [...TYPES] 因为您需要另一个可变数组来实现函数 enumType

你可能觉得不对劲。但事实并非如此,为什么? 函数 enumType 需要一个可变数组,这意味着它可以更改其内容,例如:

function enumType(v: {name: string; members: string[]): string[] {
  v.members.push('JOKER');
  return v.members;
}

export const MyType = enumType({
  name: 'MyType',
  members: TYPES as unknown as string[],
});
// You would expect MyType is ['H', 'S', 'JOKER'] but you are having an error in strict mode as you were trying to add an element to a freezed array

如果您拥有函数 enumType 并且您知道它不会更改数组内容,在这种情况下,该函数应该接受 readonly string[]

function enumType({v: {name: string; memebers: readonly string[]}) {
  ...
}

如果您没有该功能:members: [...TYPES] 是最好的方法。