1 Star 1 Fork 0

wangzichu/ts-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2693.EndsWith.ts 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
王子初 提交于 2023-04-20 18:23 . 第二次提交
/*
2693 - EndsWith
-------
by jiangshan (@jiangshanmeta) #中等 #template-literal
### 题目
实现`EndsWith<T, U>`,接收两个string类型参数,然后判断`T`是否以`U`结尾,根据结果返回`true`或`false`
例如:
```typescript
type a = EndsWith<'abc', 'bc'> // expected to be true
type b = EndsWith<'abc', 'abc'> // expected to be true
type c = EndsWith<'abc', 'd'> // expected to be false
```
> 在 Github 上查看:https://tsch.js.org/2693/zh-CN
*/
/* _____________ 你的代码 _____________ */
type EndsWith<T extends string, U extends string> =
T extends `${infer Q}${infer S}` ?
U extends `${Q}${infer N}` ?
T extends U ? true : false : EndsWith<S,U>
: false;
type test = EndsWith<'abc', 'bc'>
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<EndsWith<'abc', 'bc'>, true>>,
Expect<Equal<EndsWith<'abc', 'abc'>, true>>,
Expect<Equal<EndsWith<'abc', 'd'>, false>>,
]
/* _____________ 下一步 _____________ */
/*
> 分享你的解答:https://tsch.js.org/2693/answer/zh-CN
> 查看解答:https://tsch.js.org/2693/solutions
> 更多题目:https://tsch.js.org/zh-CN
*/
马建仓 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

搜索帮助