Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Arrays 类型tuple,以便函数链具有有效的参数和返回类型_Arrays_Typescript_Tuples - Fatal编程技术网

Arrays 类型tuple,以便函数链具有有效的参数和返回类型

Arrays 类型tuple,以便函数链具有有效的参数和返回类型,arrays,typescript,tuples,Arrays,Typescript,Tuples,我想为下面的这个元组/数组创建一个类型 在有效的情况下: const funcs = [(a: string) => 1, (a: number) => 'A', (a: string) => 2] 这是无效的: const funcs = [(a: string) => 1, (a: number) => 2, (a: string) => 3] 不同之处在于中间函数的返回类型从字符串更改为数字 可能吗 type SubtractOne<T ext

我想为下面的这个元组/数组创建一个类型

在有效的情况下:

const funcs = [(a: string) => 1, (a: number) => 'A', (a: string) => 2]
这是无效的:

const funcs = [(a: string) => 1, (a: number) => 2, (a: string) => 3]
不同之处在于中间函数的返回类型从字符串更改为数字

可能吗

type SubtractOne<T extends number> = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62][T];
type AddOne<T extends number> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62][T];

const funcs = [(a: string) => 1, (a: number) => 'A', (a: string) => 2]

type CheckFuncs<T extends any[]> = { [(K extends number) in keyof T]: T[AddOne<K>] }

type funcsType = typeof funcs
type funcsType2 = CheckFuncs<funcsType>
type SubtractOne=[-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,41,42,43,44,46,48,49,50,51,53,54,55,57,58,59,60,62][T];
类型AddOne=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,43,44,46,47,48,50,51,52,53,54,55,57,59,60,62][T];
常量funcs=[(a:string)=>1,(a:number)=>a',(a:string)=>2]
键入CheckFuncs={[(K扩展数字)在keyof T]:T[AddOne]}
type funcsType=typeof funcs
类型funcsType2=CheckFuncs
在我的研究过程中,我发现了一种使用映射进行索引的方法。是否可以使用此选项将
AddOne
添加或减去到K?然后我可以访问
T[K]
ReturnType
参数[0]


对于给定索引类型
K
的元组的下一个或上一个值进行索引是我需要支持的。考虑<代码>尾部<代码>,它采用元组类型<代码> t>代码>,并在删除第一个元素时返回一个新的元组类型:

// Tail<[1,2,3]> is [2,3]
type Tail<T extends readonly any[]> = T extends [any, ...infer R] ? R : never;
它遍历
T
,并将
T[K]
Tail[K]
进行比较,然后将
T[K]
转换为一个可以工作的版本。然后是这些(请注意,需要保留元组类型):

制作这些:

type passType = CheckFuncs<typeof pass>
// readonly [(a: string) => number, (a: number) => string, (a: string) => number]

type failType = CheckFuncs<typeof fail>
// readonly [(a: string) => number, (x: number) => string, (a: string) => number]
当然,您想要编写或使用
CheckFuncs
的确切方式可能会有所不同;这只是一个例子。希望有帮助;祝你好运


这是什么魔法的可能复制品………..我可以问一下,为什么你最终会返回
(x:A)=>S
,而不是
从不
?你可以在操场上自己测试它;如果这样做,则错误函数的错误消息是“类型为
(a:number)=>number
的参数不可分配给
never
”,这并不能真正告诉您如何修复它。与上面的错误消息相比,“
(a:number)=>number
不可分配给
(x:number)=>string
,因此您可以看到需要将返回类型更改为
string
(好,或者将下一个函数的参数更改为
number
,但是
const pass = [(a: string) => 1, (a: number) => 'A', (a: string) => 2] as const
const fail = [(a: string) => 1, (a: number) => 3, (a: string) => 2] as const
type passType = CheckFuncs<typeof pass>
// readonly [(a: string) => number, (a: number) => string, (a: string) => number]

type failType = CheckFuncs<typeof fail>
// readonly [(a: string) => number, (x: number) => string, (a: string) => number]
function useFuncs<T extends readonly ((x: any) => any)[]>(...t: CheckFuncs<T>) { }

useFuncs((a: string) => 1, (a: number) => 'A', (a: string) => 2); // okay
useFuncs((a: string) => 1, (a: number) => 3, (a: string) => 2); // error!
// ----------------------> ~~~~~~~~~~~~~~~~
// Argument of type '(a: number) => number' is not assignable to 
// parameter of type '(x: number) => string'.