一系列连续的映射功能的通用打字稿类型
我正在通过一系列功能多次映射类型 a 的对象。
例如:
- 首先具有类型
的函数(项目:a)=> b
- 然后具有类型
的函数(项目:b)=> C
- 然后具有类型
的函数(item:c)=> d
出现了类型d
的对象。这些函数作为数组传递,在示例中将具有类型[(item:a)=> b,(项目:b)=> C,(项目:C)=> d]
。
一个具体的示例:
const mappings: MappingSequence<number, { duration: string }> = [
a => a * 60, // (a: number) => number
b => `${b} seconds`, // (b: number) => string
c => ({ duration: c }), // (c: string) => { duration: string }
];
其中将所有3个函数应用于编号2
导致{持续时间:'120秒'}
导致。
我希望表达这种通用类型mappingSequence&lt; a,z&gt;
:
type Mapping<X, Y> = (item: X) => Y;
// This definition style won't work because there are infinitely many options
type MappingSequence<A, Z> =
[] | // 0-step mapping, only if A = Z
[Mapping<A, Z>] | // 1-step mapping
[Mapping<A, B>, Mapping<B, Z>] | // 2-step mapping, for any B
[Mapping<A, B>, Mapping<B, C>, Mapping<C, Z>] | // 3-step mapping, for any B and C
// etc., for arbitrary array lengths
// This style might work, but the recursion is tricky
type MappingSequence<A, Z> =
[Mapping<A, Z>] |
[Mapping<A, B>, ...MappingSequence<B, Z>]; // for any B
但是,我已经陷入了多次不同的尝试。 关键困难似乎在表达“对于任何 b”。我想使用推断
关键字来执行此操作;但是,然后我需要拥有有条件的类型,当拥有通用的a
和z
时,我发现这很难。
关于如何实现这一目标有任何见解吗?提前致谢。
I am mapping an object of type A
multiple times via a succession of functions.
For example:
- first a function with type
(item: A) => B
- then a function with type
(item: B) => C
- then a function with type
(item: C) => D
and out comes an object of type D
. These functions are passed as an array, which in the example would have type [(item: A) => B, (item: B) => C, (item: C) => D]
.
An concrete example of this:
const mappings: MappingSequence<number, { duration: string }> = [
a => a * 60, // (a: number) => number
b => `${b} seconds`, // (b: number) => string
c => ({ duration: c }), // (c: string) => { duration: string }
];
where applying all 3 functions to the number 2
results in { duration: '120 seconds' }
.
I am looking to express this generic type MappingSequence<A, Z>
:
type Mapping<X, Y> = (item: X) => Y;
// This definition style won't work because there are infinitely many options
type MappingSequence<A, Z> =
[] | // 0-step mapping, only if A = Z
[Mapping<A, Z>] | // 1-step mapping
[Mapping<A, B>, Mapping<B, Z>] | // 2-step mapping, for any B
[Mapping<A, B>, Mapping<B, C>, Mapping<C, Z>] | // 3-step mapping, for any B and C
// etc., for arbitrary array lengths
// This style might work, but the recursion is tricky
type MappingSequence<A, Z> =
[Mapping<A, Z>] |
[Mapping<A, B>, ...MappingSequence<B, Z>]; // for any B
However, I've gotten stuck in multiple different attempts.
The key difficulty seems to be expressing "for any B". I was looking to do this with the infer
keyword; however, then I need to have conditional types, which I found hard when having the generic over A
and Z
.
Any insights in how this can be achieved? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用通用功能时,这似乎可以实现:
让我们看看这是否有效。
如您所见,有一个小警告:您必须给每个函数参数一个明确的类型。 但是,如果给定类型不正确,TypeScript将出现错误。
游乐场
This seems to be possible to achieve when using a generic function:
Let's see if this works.
As you can see there is a small caveat: You have to give each function argument an explicit type. But TypeScript will give an error if the given type is incorrect.
Playground