在Typescript中使用yargs
我不明白当我将选项抽象到一个变量,甚至从另一个文件导入时,Typescript抱怨:在Typescript中使用yargs,typescript,yargs,Typescript,Yargs,我不明白当我将选项抽象到一个变量,甚至从另一个文件导入时,Typescript抱怨: Argument of type '{ exclude: { type: string; required: boolean; description: string; default: never[]; alias: string; }; someOtherFlag: { type: string; required: boolean; description: string; default: never[
Argument of type '{ exclude: { type: string; required: boolean; description: string; default: never[]; alias: string; }; someOtherFlag: { type: string; required: boolean; description: string; default: never[]; }; }' is not assignable to parameter of type '{ [key: string]: Options; }'.
Property 'exclude' is incompatible with index signature.
Type '{ type: string; required: boolean; description: string; default: never[]; alias: string; }' is not assignable to type 'Options'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"string" | "number" | "boolean" | "array" | "count" | undefined'.ts(2345)
在使用时,请首先执行以下操作之一: 说明: 当查看其类型时,传递给yargs.options的options文本看起来很好 有一点很重要,为什么它在当前表单中不起作用:options literal类型变宽了。因此,类型:“array”变为类型:string。选项要求类型为,所以在这里它爆炸了 如果您想了解更多关于此主题的内容,则上述类型加宽主要是因为缺少显式类型和显式类型
import * as yargs from 'yargs';
const options = {
exclude: {
type: 'array',
required: false,
description: 'Files to exclude',
default: [],
alias: 'e'
},
someOtherFlag: {
type: 'array',
required: false,
description: 'Another example flag'
default: []
}
};
// throws Typescript error
const cliOptions = yargs.options(options).argv;
const options = {...} as const
// or
const options = {
exclude: { type: "array" as "array", ...},
someOtherFlag: { type: "array" as "array", ...}
}