具有多种类型和类型化数组的 Vuejs 道具验证
Vuejs prop validation with multiple types and typed arrays
使用 Vuejs 3 和 Typescript,目标是让道具成为 string | string[]
类型。查看 the docs,看起来我应该能够使用多种类型设置道具,如下所示:
props: {
prop1: {type: [String, Array], default: ""}
}
查看 Vetur commit and 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
之间有什么不同,但我对那里的某些东西非常不满意。我也对错误中提到的未知内容感到有些困惑,因为我希望所有内容都是字符串或数组。
这似乎有效:
props: {
prop1: {type: [Array, String] as PropType<string[] | string>, default: ""}
}
看来你必须转换整个类型,否则你会收到警告。此外,似乎 Array
默认为 uknown[]
类型
使用 Vuejs 3 和 Typescript,目标是让道具成为 string | string[]
类型。查看 the docs,看起来我应该能够使用多种类型设置道具,如下所示:
props: {
prop1: {type: [String, Array], default: ""}
}
查看 Vetur commit and 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
之间有什么不同,但我对那里的某些东西非常不满意。我也对错误中提到的未知内容感到有些困惑,因为我希望所有内容都是字符串或数组。
这似乎有效:
props: {
prop1: {type: [Array, String] as PropType<string[] | string>, default: ""}
}
看来你必须转换整个类型,否则你会收到警告。此外,似乎 Array
默认为 uknown[]
类型