1 Star 1 Fork 0

wangzichu/ts-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
4518.Fill.ts 2.83 KB
一键复制 编辑 原始数据 按行查看 历史
王子初 提交于 2023-04-23 13:31 . 提交
/*
* @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @Date: 2023-04-21 16:27:16
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2023-04-21 16:58:37
* @FilePath: /ts-challenges/4518.Fill.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
/*
4518 - Fill
-------
by キリサメ qianxi (@qianxi0410) #medium #tuple
### Question
`Fill`, a common JavaScript function, now let us implement it with types.
`Fill<T, N, Start?, End?>`, as you can see,`Fill` accepts four types of parameters, of which `T` and `N` are required parameters, and `Start` and `End` are optional parameters.
The requirements for these parameters are: `T` must be a `tuple`, `N` can be any type of value, `Start` and `End` must be integers greater than or equal to 0.
```ts
type exp = Fill<[1, 2, 3], 0> // expected to be [0, 0, 0]
```
In order to simulate the real function, the test may contain some boundary conditions, I hope you can enjoy it :)
> View on GitHub: https://tsch.js.org/4518
*/
/* _____________ Your Code Here _____________ */
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Arr extends unknown[] = [],
bol extends boolean = false
> = Start extends End ? T : Arr['length'] extends T['length'] ? Arr : Arr['length'] extends Start ?
Fill<T,N,Start,End,[...Arr,N],true> : bol extends true ? Arr['length'] extends End ?
Fill<T,N,Start,End,Arr,false> : Fill<T,N,Start,End,[...Arr,N],true> : Fill<T,N,Start,End,[...Arr,T[Arr['length']]],false>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Fill<[], 0>, []>>,
Expect<Equal<Fill<[], 0, 0, 3>, []>>,
Expect<Equal<Fill<[1, 2, 3], 0, 0, 0>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], 0, 2, 2>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], 0>, [0, 0, 0]>>,
Expect<Equal<Fill<[1, 2, 3], true>, [true, true, true]>>,
Expect<Equal<Fill<[1, 2, 3], true, 0, 1>, [true, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 1, 3>, [1, true, true]>>,
Expect<Equal<Fill<[1, 2, 3], true, 10, 0>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 10, 20>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 0, 10>, [true, true, true]>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/4518/answer
> View solutions: https://tsch.js.org/4518/solutions
> More Challenges: https://tsch.js.org
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wang-zichu/ts-challenges.git
git@gitee.com:wang-zichu/ts-challenges.git
wang-zichu
ts-challenges
ts-challenges
master

搜索帮助