在 Typescript 中,为数组定义一个类型,其中第一个元素比其余元素更具体 [英] In Typescript, define a type for an array where the first element is more specific than the rest

查看:32
本文介绍了在 Typescript 中,为数组定义一个类型,其中第一个元素比其余元素更具体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个数组定义一个类型,它的第一个元素是特定类型(例如函数),其余元素是空类型.例如:

I would like to define a type for an array whose first element is a specific type (e.g. Function), and the remaining elements are the empty type. For example:

type FAs = [Function, {}, {}, {}, ...]; // pseudo code

这种事情有可能吗?

目的是提供这样的单参数函数:

The purpose is to provide a single-argument function like this:

const myCaller = ([fun, ...args]: FAs) => fun.apply(args);

另一种方法是对 myCaller 使用两个参数,如下所示:

An alternative approach would be to use two arguments to myCaller, like this:

const myCaller = (fun: Function, args: any[]) => fun.apply(args);

但出于审美原因,我更愿意使用单个参数.我还想知道类型系统是否支持可以说是任意长度的元组.也许出于我不明白的计算机科学原因,这样的事情是不可取的.

but for aesthetic reasons I would prefer to use a single argument. I also wonder if the type system supports what is arguably an arbitrary-length tuple. Maybe such a thing is undesirable for computer science reasons I don't understand.

推荐答案

如果你定义

type FAs = [Function, {}];

然后 FAs 类型的值将需要 Function 类型的第一个元素,{} 类型的第二个元素,以及后面的元素<代码>功能 |{}.这就是 TypeScript 文字数组类型的工作原理.来自 TS 文档:

Then values of type FAs will require a first element of type Function, a second element of type {}, and succeeding elements of Function | {}. That is how TypeScript literal array types work. From the TS docs:

当访问已知索引集之外的元素时,使用联合类型代替:

When accessing an element outside the set of known indices, a union type is used instead:

这应该可以完成您想要的一切除了,因为您将能够传入一个 Function 类型的值作为数组的第三个元素等.但实际上无论如何都是如此,因为 Function{} 兼容.

This should do everything you want except for the fact that you will be able to pass in a Function-typed value as the third element etc. of the array. But actually that would be the case anyway, since Function is compatible with {}.

没有办法解决这个问题.在 TS 中没有办法定义一个数组类型,其中前 n 个元素是某种特定类型,并且有任意数量的其他特定类型的剩余元素.

There is no way around this. There is no way in TS to define an array type where the first n elements are of some specific type(s), and there are an arbitrary number of remaining elements of some other specific type.

我还想知道类型系统是否支持可以说是任意长度的元组.

I also wonder if the type system supports what is arguably an arbitrary-length tuple.

实际上,类型系统支持任意长度的元组.如果你说

Actually, the type system only supports arbitrary-length tuples. If you say

type Tuple = [number, number];

此类型与包含数字的任何长度为 2 或更大的数组兼容.如果你说

this type is compatible with any array, of length two or greater, that contains numbers. If you say

type Tuple = [string, number];

此类型与任何长度为 2 或更长的数组兼容,这些数组的第一个元素是字符串,第二个元素是数字,第三个元素是字符串或数字,等等.我不会将这种行为的原因称为基于计算机科学";更重要的是 TS 检查什么是可行的.

this type is compatible with any array, of length two or longer, that has a string as its first element, a number as its second, and either a string or number as its third etc. I would not call the reasons for this behavior "computer-science based"; it's more a matter of what it's feasible for TS to check.

interface Arglist {
  [index: number]: object;
  0: Function;
}

const a1: Arglist = [func];
const a2: Arglist = [22];                  // fails
const a3: Arglist = [func, "foo"];         // fails
const a4: Arglist = [func, obj];
const a5: Arglist = [func, obj, obj];

这篇关于在 Typescript 中,为数组定义一个类型,其中第一个元素比其余元素更具体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆