nya*_*han 5 javascript arrays types type-inference typescript
我有一个像这样的数组:
const arr = ['a', 'b'] as const;
// type: readonly ["a", "b"]
Run Code Online (Sandbox Code Playgroud)
现在我想加入数组中的字符串:
const joined = arr.join('');
// type: string
Run Code Online (Sandbox Code Playgroud)
我的目标是编写一个函数,将字符串连接到数组中并将它们作为文字字符串类型 ( "ab") 返回。
有人知道这是否可能吗?我认为应该如此,因为所有需要的类型信息都已给出。我不期望问题得到完整的解决——也许只是进一步研究的暗示就很好了。
小智 -1
const joinedString = (arr as string[]).join('');
console.log(joinedString); // Output: 'ab'
Run Code Online (Sandbox Code Playgroud)
这应该有效。
| 归档时间: |
|
| 查看次数: |
983 次 |
| 最近记录: |