使用Vuejs 3和Typescript,目标是让道具成为类型
string | string[]
. 看着
the docs
,看起来我应该可以使用多种类型设置道具,如下所示:
props: {
prop1: {type: [String, Array], default: ""}
}
看着一个
Vetur commit
和
the docs again
,看起来我应该可以设置一个数组属性类型,如下所示:
props: {
prop1: {type: Array as PropType<string[]>, default: []}
}
我可以让这两个单独工作没问题,但当我试图将它们组合起来时,我得到了一个错误:
props: {
prop1: {type: [String, Array as PropType<string[]>], default: ""}
}
No overload matches this call.
The last overload gave the following error.
Type '{ type: (StringConstructor | (new (...args: any[]) => string[] & object) | (() => string[]) | PropConstructor<string[]>[])[]; default: string; }' is not assignable to type 'PropOptions<unknown, unknown> | (new (...args: any[]) => object) | (() => unknown) | PropConstructor<unknown>[] | null'.
Types of property 'type' are incompatible.
Type '(StringConstructor | (new (...args: any[]) => string[] & object) | (() => string[]) | PropConstructor<string[]>[])[]' is not assignable to type 'true | (new (...args: any[]) => object) | (() => unknown) | PropConstructor<unknown>[] | null | undefined'.
Type '(StringConstructor | (new (...args: any[]) => string[] & object) | (() => string[]) | PropConstructor<string[]>[])[]' is not assignable to type 'PropConstructor<unknown>[]'.
Type 'StringConstructor | (new (...args: any[]) => string[] & object) | (() => string[]) | PropConstructor<string[]>[]' is not assignable to type 'PropConstructor<unknown>'.
Type 'PropConstructor<string[]>[]' is not assignable to type 'PropConstructor<unknown>'.
Type 'PropConstructor<string[]>[]' is not assignable to type '() => unknown'.
Type 'PropConstructor<string[]>[]' provides no match for the signature '(): unknown'.
我不确定定义类型的数组语法和只使用
PropType
但我对那里的事情很不满意。我还对错误中提到的未知内容有点困惑,因为我希望所有内容都是字符串或数组。