Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
Typescript 声明一个作为起点为1的数组的接口_Typescript_Typescript Typings - Fatal编程技术网

Typescript 声明一个作为起点为1的数组的接口

Typescript 声明一个作为起点为1的数组的接口,typescript,typescript-typings,Typescript,Typescript Typings,长话短说,由于复杂的原因,我需要能够声明一个使用数组签名从1开始的接口,或者至少让[0]为空。这可能吗 我已经试过了: interface ItemCollection<T> extends Collection { [index: number]: [null, ...Function[]]; addComp(): void; } 这很好用,但不一样。明白了。但它需要很多递归魔法: 首先,让我们声明UTIL以从元组中添加/删除元素: 键入PrependTupl

长话短说,由于复杂的原因,我需要能够声明一个使用数组签名从1开始的接口,或者至少让[0]为空。这可能吗

我已经试过了:

interface ItemCollection<T> extends Collection
{
    [index: number]: [null, ...Function[]];


    addComp(): void;
}

这很好用,但不一样。

明白了。但它需要很多递归魔法:

首先,让我们声明UTIL以从元组中添加/删除元素:


键入PrependTuple

我想这就是您要找的。。。也可将此标记为接受< /P>
type Unshift<T extends any[]> =
    ((a: null, ...b: T) => void) extends ((...ab: infer R) => void)
    ? R : never

type foo = Unshift<[1, 2, 3]> // [null, 1, 2, 3]

这是一种语言特性,您可能知道,在Javascript和TypeScript中,索引从零开始。你需要创建一个函数来实现你所期望的行为。是的,我想情况就是这样。希望对类型声明文件进行一些有趣的bug,以便在尝试访问索引0处的数组时引发错误,但我想不会。出于某种原因,我正在处理的API决定了,去他妈的,他们的一些数组从0开始,其他的从1开始。。。
tmp: [null, ...Function[]];
type Iter<N extends number, Items extends any[], L extends Array<any> = []> = {
  true: L;
  false: Iter<N, Items, PrependTuple<L['length'] extends N ? unknown : Items[Decrease<L['length']>]  , L>>
}[L["length"] extends N ? "true" : "false"];
type Unshift<T extends any[]> =
    ((a: null, ...b: T) => void) extends ((...ab: infer R) => void)
    ? R : never

type foo = Unshift<[1, 2, 3]> // [null, 1, 2, 3]