[Typescript] 121. Hard - IsPalindrome
typescript Hard 121
2023-09-14 08:59:11 时间
Implement type IsPalindrome<T>
to check whether a string or number is palindrome.
For example:
IsPalindrome<'abc'> // false
IsPalindrome<121> // true
/* _____________ Your Code Here _____________ */
type Equal<T, U> = (<P>(x: P) => P extends T? 1: 2) extends (<P>(x: P) => P extends U ? 1: 2) ? true: false;
type ToArray<S extends string> = S extends `${infer First}${infer REST}` ? [First, ...ToArray<REST>]: [];
type ReverseJoin<T extends any[]> = T extends [...infer REST, infer Last] ? Last extends string ? `${Last}${ReverseJoin<REST>}` : never: '';
type IsPalindrome<T extends string | number> = Equal<`${T}`,ReverseJoin<ToArray<`${T}`>>>
/* _____________ Test Cases _____________ */
import type { Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<IsPalindrome<'abc'>, false>>,
Expect<Equal<IsPalindrome<'b'>, true>>,
Expect<Equal<IsPalindrome<'abca'>, false>>,
Expect<Equal<IsPalindrome<'abcba'>, true>>,
Expect<Equal<IsPalindrome<121>, true>>,
Expect<Equal<IsPalindrome<19260817>, false>>,
]
相关文章
- 【TypeScript】中的数组和元组之间的关系
- 简单的贪吃蛇小游戏—TypeScript练习
- 【说站】微软官方发布TypeScript 4.4版本
- 记一次vscode踩坑记录:"TypeScript 语言服务在其启动后已中止 5 次。将不会重启该服务。"
- 【项目总结】TypeScript基础
- TypeScript 深水区:3 种类型来源和 3 种模块语法
- Java和TypeScript开发者之间的区别必须知道
- TypeScript 中 as const 是什么
- 一道 3 层的 TypeScript 面试题,你能答到第几层?
- 开发者数量2025年预计破亿,TypeScript受欢迎度飙升:GitHub最新年度报告
- 在TypeScript中使用装饰器