Replace First
约 150 字小于 1 分钟
2026-02-11
题目
Implement the type ReplaceFirst<T, S, R> which will replace the first occurrence of S in a tuple T with R. If no such S exists in T, the result should be T.
解题思路
待补充
答案
type ReplaceFirst<T extends readonly unknown[], S, R> = any验证
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<ReplaceFirst<[1, 2, 3], 3, 4>, [1, 2, 4]>>,
Expect<Equal<ReplaceFirst<['A', 'B', 'C'], 'C', 'D'>, ['A', 'B', 'D']>>,
Expect<Equal<ReplaceFirst<[true, true, true], true, false>, [false, true, true]>>,
Expect<Equal<ReplaceFirst<[string, boolean, number], boolean, string>, [string, string, number]>>,
Expect<Equal<ReplaceFirst<[1, 'two', 3], string, 2>, [1, 2, 3]>>,
Expect<Equal<ReplaceFirst<['six', 'eight', 'ten'], 'eleven', 'twelve'>, ['six', 'eight', 'ten']>>,
]参考
无
