Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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 是否有实用程序类型将元组类型中的所有元素转换为接受TypeScript中的“undefined”?_Javascript_Typescript - Fatal编程技术网

Javascript 是否有实用程序类型将元组类型中的所有元素转换为接受TypeScript中的“undefined”?

Javascript 是否有实用程序类型将元组类型中的所有元素转换为接受TypeScript中的“undefined”?,javascript,typescript,Javascript,Typescript,是否有实用程序类型将元组类型中的所有元素转换为接受未定义的 输入 预期产量 type OptionalInputType = [string|undefined, number|undefined] 可以使用生成新数组和元组。这意味着您可以定义: type MapUnionWithUndefined<T> = { [K in keyof T]: T[K] | undefined }; 如果要接受长度为0、1或2的元组,可以使用内置的: 可以使用生成新数组和元组。这意味着您可以定义

是否有实用程序类型将元组类型中的所有元素转换为接受未定义的

输入

预期产量

type OptionalInputType = [string|undefined, number|undefined]
可以使用生成新数组和元组。这意味着您可以定义:

type MapUnionWithUndefined<T> = { [K in keyof T]: T[K] | undefined };
如果要接受长度为0、1或2的元组,可以使用内置的:

可以使用生成新数组和元组。这意味着您可以定义:

type MapUnionWithUndefined<T> = { [K in keyof T]: T[K] | undefined };
如果要接受长度为0、1或2的元组,可以使用内置的:


感谢@jcalz,没有意识到映射的实用程序类型对tuple的作用和magic一样好。感谢@jcalz,没有意识到映射的实用程序类型对tuple的作用和magic一样好。
type OptionalInputType = MapUnionWithUndefined<InputType>;
// type OptionalInputType = [string | undefined, number | undefined]
const okay: OptionalInputType = [undefined, undefined]; // okay
const boo: OptionalInputType = []; // error!
type TrulyOptionalInputType = Partial<InputType>;
// type TrulyOptionalInputType = [(string | undefined)?, (number | undefined)?]

const nowOkay: TrulyOptionalInputType = []; // okay