1 Star 1 Fork 0

wangzichu/ts-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2059.DropString.ts 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
王子初 提交于 2023-08-02 16:18 . 更新题库
/*
2059 - Drop String
-------
by CaptainOfPhB (@CaptainOfPhB) #hard #template-literal #infer
### Question
Drop the specified chars from a string.
For example:
```ts
type Butterfly = DropString<'foobar!', 'fb'> // 'ooar!'
```
> View on GitHub: https://tsch.js.org/2059
*/
/* _____________ Your Code Here _____________ */
type ConvertStringToUnionType<R extends string,S extends string | undefined = undefined> = R extends `${infer L}${infer N}` ? N extends '' ? S | L : ConvertStringToUnionType<N,S | L> : never
type test = ConvertStringToUnionType<'1234'>
type DropString<S extends string, R extends string> = S extends `${infer L}${ConvertStringToUnionType<R>}${infer N}` ? DropString<`${L}${N}`,R> : S
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<DropString<'butter fly!', ''>, 'butter fly!'>>,
Expect<Equal<DropString<'butter fly!', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<'butter fly!', 'but'>, 'er fly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' butter fly! ', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'tub'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'b'>, ' u t t e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 't'>, ' b u e r f l y ! '>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/2059/answer
> View solutions: https://tsch.js.org/2059/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

搜索帮助