English 中文(简体)
Variadic zip over iterable in Format
原标题:Variadic zip over iterables in TypeScript

I m试图在保持类型的同时,用手法书写字.。 例如,

function* naturals(max=10) { for (let i=0; i<max; i++) yield i }
const x = [1, 2, 3]
const y = ["a", "b", "c"]

const zipped = zip(naturals(), x, y)
console.log([...zipped]) // => [[0, 1, "a"], [1, 2, "b"], [2, 3, "c"]]

function* zip<?>(...iterables:?[]) {
   const iterators = iterables.map(iter => iter[Symbol.iterator]())
   ...
}

是否有有助于保护我类型的签字?

最佳回答

页: 1 下列签名:

declare function zip<T extends any[]>(
  ...iterables: { [I in keyof T]: Iterable<T[I]> }
): Iterable<T>;

http://www.typescriptlang.org/docs/handbook/2/generics.html 预计这将成为tuple

输入类型是: 定额/a>,在阵列/tuple打上打上的一个地图类型T>>/code>,其中每一部分>。 该地图型号为homo吗?em> (featuring in keyof T,见。 什么是“吗 map”的类型?用于更多的信息,因此,“样式”可从中引用<条码>。

让我举一个例子:

const zipped = zip(naturals(), x, y)
// const zipped: Iterable<[number, number, string]>

此处iterables 是可转让至的直观型号:[可检索与设计;编号与设计;编号与编号;编号与“;可复制与设计;string>],因此汇编者载于T。 因此,产出类型为:<代码>易读和;[编号、编号、编号]>。

的游乐联系

问题回答

暂无回答




相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to generate an instance of an unknown type at runtime?

i ve got the following in C#: string typename = "System.Int32"; string value = "4"; theses two strings should be taken to generate an object of the specified type with the specified value... result ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...