type ReplaceFirst<T extends readonly unknown[], S, R, A extends any[] = []> = T extends [infer F, ...infer Rest]
? F extends S
? [...A, R, ...Rest]
: ReplaceFirst<Rest, S, R, [...A, F]>
: A;
Solution by wendao-liu #35147
// ζΉζ³δΈ
type ReplaceFirst<T extends readonly unknown[], S, R, U extends unknown[] = []> = T extends [infer V, ...infer rest] ?
V extends S ? [...U, R, ...rest] : ReplaceFirst<rest, S, R, [...U, V]> : U
// ζΉζ³δΊ
type ReplaceFirst<T extends readonly unknown[], S, R> = T extends [infer V, ...infer rest] ? V extends S ? [R, ...rest] : [V, ...ReplaceFirst<rest, S, R>] : T
Solution by ouzexi #34149
// your answers
type ReplaceFirst<T extends readonly unknown[], S, R> = T extends [infer F , ...infer Rest]
? F extends S
? [R, ...Rest]
: [F, ...ReplaceFirst<Rest, S, R>]
: []
Solution by pea-sys #33612
type ReplaceFirst<
T extends readonly unknown[],
S,
R,
> =
T extends [infer First, ...infer Rest]
? First extends S
? [R, ...Rest]
: [First, ...ReplaceFirst<Rest, S, R>]
: []
Solution by sunupupup #33480
// your answersnd we are happy to see you'd like to share your solutions!
However, due to the increasing number of users, the issue pool would be filled by answers very quickly.
Before you submit your solutions, please kindly search for similar solutions that may already be posted. You can "thumb up" on them or leave your comments on that issue. If you think you have a different solution, do not hesitate to create the issue and share it with others. Sharing some ideas or thoughts about how to solve this problem is greatly welcome!
Thanks!
-->
```ts
type ReplaceFirst<T extends readonly unknown[], S, R, SaveArr extends any[] =[],Trigger extends boolean = false> = Equal<Trigger,true> extends true
? [...SaveArr,...T]
:T extends [infer first, ...infer rest]
? first extends S
? ReplaceFirst<rest,S,R,[...SaveArr,R],true>
: ReplaceFirst<rest,S,R,[...SaveArr,first],false>
:SaveArr
Solution by bananana0118 #33172
type ReplaceFirst<T extends readonly unknown[], S, R, C extends unknown[] = []> = T extends [infer F, ...args: infer O]
? F extends S
? [...C, R, ...O]
: ReplaceFirst<O, S, R, [...C, F]>
: C
Solution by heavenzhen999 #33140
type ReplaceFirst<T extends readonly unknown[], S, R> =
T extends [infer F, ...infer Rest]
? F extends S
? [R, ...Rest]
: [F, ...ReplaceFirst<Rest, S, R>]
: []
Solution by teamchong #33057
type ReplaceFirst<T extends readonly unknown[], S, R> = T extends [infer First, ...infer Rest]
? First extends S
? [R, ...Rest]
: [First, ...ReplaceFirst<Rest, S, R>]
: T
Solution by kanishev #33016
type ReplaceFirst<T extends any[], S, R, U extends any[] = []> = T extends [
infer F,
...infer L
]
? F extends S
? [...U, R, ...L]
: ReplaceFirst<L, S, R, [...U, F]>
: U;
type _ReplaceFirst<T extends any[], S, R> = T extends [infer F, ...infer L]
? F extends S
? [R, ...L]
: [F, ..._ReplaceFirst<L, S, R>]
: [];
Solution by moonpoet #31039
// your answers
type ReplaceFirst<T extends readonly unknown[], S, R> =
T extends [infer F, ...infer L]
? F extends S
? [R, ...L]
: [F, ...ReplaceFirst<L, S, R>]
: []
Solution by milletlovemouse #30951
type ReplaceFirst<T extends unknown[], S extends unknown, R extends unknown, V extends unknown[] = []> = T extends [
infer F,
...infer Rest,
]
? F extends S
? ReplaceFirst<Rest, never, R, [...V, R]>
: ReplaceFirst<Rest, S, R, [...V, F]>
: V;
Solution by leejaehyup #30878
type ReplaceFirst<T extends readonly unknown[], S, R,> = T extends [infer A,...infer B] ? A extends S ? [R, ...B] : [A, ...ReplaceFirst<B, S, R>] : T
Solution by dreamluo-plus #30697
type ReplaceFirst<
T extends readonly unknown[],
S,
R,
RES extends unknown[] = [],
> = T extends [infer HEAD, ...infer TAIL]
? HEAD extends S
? [...RES, R, ...TAIL]
: ReplaceFirst<TAIL, S, R, [...RES, HEAD]>
: RES
Solution by playitsafe #30420
type ReplaceFirst<T extends readonly unknown[], S, R> = T extends [infer Head, ...infer Tail]
? Head extends S
? [R, ...Tail]
: Head extends any[] ? [...Head] : [Head, ...ReplaceFirst<Tail, S, R>]
: T;
Solution by duynhanf #29345
// your answers
type ReplaceFirst<T extends readonly unknown[], S, R> =
T extends [infer FI , ...infer Rest] ?
FI extends S ? [R, ...Rest]
:
[FI ,...ReplaceFirst<Rest, S, R > ]
: T
Solution by kerolossamir165 #28977
type ReplaceFirst<T extends readonly unknown[], S, R> = T extends [
infer Item,
...infer Rest
]
? Item extends S
? [R, ...Rest]
: [Item, ...ReplaceFirst<Rest, S, R>]
: [];
Solution by DoubleWoodLin #28901
type ReplaceFirst<T extends readonly unknown[], S, R, Arr extends unknown[] = []> = T extends [infer F,...infer REST] ? F extends S ? [...Arr, R, ...REST] : ReplaceFirst<REST, S, R, [...Arr, F]> : Arr;
Solution by kekekgwj #28862
θΏι’ηζζζ―οΌζΏζ’ζζ°η» T δΈη第δΈδΈͺ S δΈΊ RοΌε ζ€ζ们ιεζ°η»οΌε° S ζΏζ’ε³ε―
θΏιη¨ Res ζ₯δΏεη»ζζ°η»οΌε δΈΊιθ¦ζΉεζ°η»ηεΌοΌιθ¦ηζζ°ηζ°η»οΌζ Res ε Rest θΏζ R η»ζζ°ηζ°η»ε³ε―
type ReplaceFirst<T extends readonly unknown[], S, R, Res extends readonly unknown[] = []> = T extends [infer F, ...infer Rest]
? F extends S
? [...Res, R, ...Rest]
: ReplaceFirst<Rest, S, R, [...Res, F]>
: Res
Solution by linjunc #28361
type ReplaceFirst<
T extends readonly unknown[],
S,
R,
U extends unknown[] = []
> = T extends [infer F, ...infer Rest]
? F extends S
? // if F extends SοΌU is all the members that have been traversed before and Rest is all the members that have not been traversed yet
[...U, R, ...Rest]
: // if F not extends SοΌ push F into U
ReplaceFirst<Rest, S, R, [...U, F]>
: U;
Solution by jiaowoxiaobala #28045
type ReplaceFirst<T extends any[], S, R> = T extends [infer F, ...infer Rest]
? F extends S
? [R, ...Rest]
: [F, ...ReplaceFirst<Rest, S, R>]
: T;
Solution by slemchik03 #27619
type ReplaceFirst<T extends readonly unknown[], S, R, Appeared extends unknown[] = []> =
T extends [infer A, ...infer Rest]
? A extends S
? [...Appeared, R, ...Rest]
: ReplaceFirst<Rest, S, R, [...Appeared, A]>
: Appeared
Solution by drylint #27402
type ReplaceFirst<T extends readonly unknown[], S, R, M extends unknown[] = []> =
T extends [infer F, ...infer Rest] ? F extends S ? [...M, R, ...Rest] : ReplaceFirst<Rest, S, R, [...M, F]> : M
Solution by smileboyi #27276
type ReplaceFirst<T extends readonly unknown[], S, R, Result extends unknown[] = []> = T extends [
infer First,
...infer Rest,
]
? First extends S
? [...Result, R, ...Rest]
: ReplaceFirst<Rest, S, R, [...Result, First]>
: Result;
Solution by rldnd #27038
type ReplaceFirst<T extends readonly unknown[], S, R, Res extends unknown[] = []> = T extends [infer First, ...infer Rest]
? (First extends S
? [...Res, R, ...Rest]
: ReplaceFirst<Rest, S,R,[...Res, First]>)
: [...Res, ...T]
Solution by nikitashevchenkodp #26672
type ReplaceFirst<T extends readonly unknown[], S, R> = T extends [
infer F,
...infer REST
]
? F extends S
? [R, ...REST]
: [F, ...ReplaceFirst<REST, S, R>]
: T;
Solution by JohnLi1999 #26431
// your answers
type ReplaceFirst<T extends any[], S, R> = T extends [infer F, ...infer Rest]
? F extends S
? [R, ...Rest]
: [F, ...ReplaceFirst<Rest, S, R>]
: [];
Solution by DvYinPo #26175
type ReplaceFirst<T extends readonly unknown[], S, R, M extends unknown[] = []> = T extends [infer F, ...infer Rest]
? F extends S
? [...M, R, ...Rest]
: ReplaceFirst<Rest, S, R, [...M, F]>
: M
Solution by XkSuperCool #26097
// your answers
type ReplaceFirst<T extends readonly unknown[], S, R, Visited extends unknown[] = []> =
T extends [infer X, ...infer Y]
? X extends S
? [...Visited, R, ...Y]
: ReplaceFirst<Y, S, R, [...Visited, X]>
: Visited
Solution by kiki-zjq #25849
type ReplaceFirst<T extends readonly unknown[], S extends unknown, R extends unknown> = T extends [infer First, ...infer Rest]
? First extends S
? [R, ...Rest]
: [First, ...ReplaceFirst<Rest, S, R>]
: T
Solution by NeylonR #25707
type ReplaceFirst<T extends readonly unknown[], S extends unknown, R extends unknown> =
T extends [infer Head, ...infer Tail] ? (Head extends S ? [R, ...Tail] : [Head, ...ReplaceFirst<Tail, S, R>]) : T;
Solution by kanishev #25607