下面是我的代码:
export type CommandOptionType = string | number | boolean;
export type CommandOption = Record<string, CommandOptionType>;
export type LeafCommand<T extends CommandOption> = {
name: string;
handle: (
interactionOptionValues: { [K in keyof T]: T[K] },
) => Promise<void>;
};
export type LeafCommandGroup = {
name: string;
commands: Record<string, LeafCommand<CommandOption>>;
};
export type RootCommand = {
name: string;
commands: Record<string, LeafCommandGroup | LeafCommand<CommandOption>>;
};
这些类型的使用:
type OPTIONS = {
option1: "type1" | "type2" | "type3" | "type4";
option2: string;
option3: boolean;
};
const Type = {
name: "type",
handle: async (interactionOptionValues) => {
// business logic here
},
} satisfies LeafCommand<OPTIONS>;
错误发生在代码段下面:
const Root = {
name: "root",
commands: { // error occurs here
[Type.name]: Type,
},
} satisfies RootCommand;
这里,我得到的错误是Type { [x: string]: CommandOptionType } is missing the following properties from type. option1, option2, option3
.我不知道为什么Typescribe抱怨其他索引键也是字符串.我找不到工作.
期望的结果是,我应该能够添加命令,其中有LeafCommand类型没有一个拥挤.
如果你有任何其他更聪明的设计模式,我会为它.
谢谢你抽出时间