Typescript `Array.join` 文字返回类型

问题描述 投票:0回答:1

我有一个像这样的数组:

const arr = ['a', 'b'] as const;
// type: readonly ["a", "b"]

现在我想加入数组中的字符串:

const joined = arr.join('');
// type: string

我的目标是编写一个函数,将字符串连接到数组中并将它们作为文字字符串类型返回(

"ab"
)。

有人知道如果这可能吗?我认为应该如此,因为所有需要的类型信息都已给出。我不期望问题得到完整的解决 - 也许只是进一步研究的提示就很好了。

javascript arrays typescript types type-inference
1个回答
0
投票

const joinString = (arr as string[]).join(''); console.log(joinedString); // 输出:'ab'

这应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.