Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用多种类型和类型化数组进行Vuejs道具验证_Javascript_Typescript_Vue.js_Vuejs3 - Fatal编程技术网

Javascript 使用多种类型和类型化数组进行Vuejs道具验证

Javascript 使用多种类型和类型化数组进行Vuejs道具验证,javascript,typescript,vue.js,vuejs3,Javascript,Typescript,Vue.js,Vuejs3,使用Vuejs 3和Typescript,目标是使道具的类型为string | string[]。现在看来,我应该能够使用多种类型设置道具,如下所示: props: { prop1: {type: [String, Array], default: ""} } 看一个和,看起来我应该能够像这样设置一个数组道具类型: props: { prop1: {type: Array as PropType<string[]>, default: []} }

使用Vuejs 3和Typescript,目标是使道具的类型为
string | string[]
。现在看来,我应该能够使用多种类型设置道具,如下所示:

props: {
    prop1: {type: [String, Array], default: ""}
}
看一个和,看起来我应该能够像这样设置一个数组道具类型:

props: {
    prop1: {type: Array as PropType<string[]>, default: []}
}
道具:{
prop1:{type:数组作为PropType,默认值:[]}
}
我可以让这两个单独工作,没有问题,但当我尝试将它们组合在一起时,会出现一个错误:

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'.
道具:{
prop1:{type:[字符串,数组为PropType],默认值:}
}
没有与此调用匹配的重载。
上一次重载导致以下错误。
类型“{Type:(StringConstructor |”(new(…args:any[])=>string[]和object)|(()=>string[])| PropConstructor[])[];默认值:string;}”不能分配给类型“PropOptions |(new(…args:any[])=>object|(()=>unknown)| PropConstructor[]| null”。
属性“type”的类型不兼容。
类型“(StringConstructor |(新(…args:any[])=>string[]和object)|(()=>string[])PropConstructor[])[]”不能分配给类型“true”|(新(…args:any[])=>object)(()=>unknown)| PropConstructor[]| null |未定义”。
类型“(StringConstructor |(新(…参数:any[])=>string[]和object)|(()=>string[])PropConstructor[])[]”不能分配给类型“PropConstructor[]”。
类型“StringConstructor |”(新(…参数:any[])=>string[]和object)|(()=>string[])| PropConstructor[])不可分配给类型“PropConstructor”。
类型“PropConstructor[]”不可分配给类型“PropConstructor”。
类型“PropConstructor[]”不可分配给类型“()=>unknown”。
类型“PropConstructor[]”与签名“():unknown”不匹配。
我不确定用于定义类型的数组语法与直接使用
PropType
的数组语法有什么不同,但它对此感到非常不高兴。我对错误中提到的未知内容也有点困惑,因为我希望所有内容都是字符串或数组。

这似乎有效:

props: {
    prop1: {type: [Array, String] as PropType<string[] | string>, default: ""}
}
道具:{
prop1:{type:[数组,字符串]作为PropType,默认值:}
}

看起来您必须强制转换整个类型,否则会收到警告。另外,似乎
Array
默认为
uknown[]
type

默认值应该是返回空数组的回调,这是使用
type:[]
语法所必需的吗?在其他位置,默认值当然可以是普通字符串。另外,将其更改为:
default:()=>[]
会给TS带来相同的错误,我相信您可以这样做,但是对于js idkDoes
prop1:{type:[String,Array],default:():String | String[]=>'}
work?@tao不太可能。它使类型成为
string | undefined[]
,这不是我要找的。