首页 > 其他分享 >[Typescript] 121. Hard - IsPalindrome

[Typescript] 121. Hard - IsPalindrome

时间:2022-11-29 15:22:17浏览次数:35  
标签:Typescript false true IsPalindrome 121 extends Expect type

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,false,true,IsPalindrome,121,extends,Expect,type
From: https://www.cnblogs.com/Answer1215/p/16935482.html

相关文章

  • TypeScript应该怎么学?
    作者|慕课网精英讲师Lison如果自学TypeScript,到底要如何学呢?1.2.1学会看文档英文官方文档始终是及时更新的。但即便是官方的文档,有一些更新在更新日志里写了,而新手指南里......
  • 我要涨知识——TypeScript 常见面试题(二)
    又是一个年底来了,好大一批人可能又准备跑路了,最近回家待产,翻了翻掘金和CSDN发现好多大佬都有大厂Offer,看着看着我心动了!话不多说,赶紧开干,给自己整了一个前端面试小助......
  • 0121-Go-字符串格式化
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/string-formatting目标使用Go语言的字符串格式化。示例packagemainimport("fmt"......
  • typeScript
    基础类型布尔值最基本的数据类型就是简单的true/false值,在JavaScript和TypeScript里叫做boolean(其它语言中也一样)。letisDone:boolean=false;数字和JavaScript一......
  • [Typescript] 120. Hard - ObjectFromEntries
    Implementthetypeversionof Object.fromEntriesForexample:interfaceModel{name:string;age:number;locations:string[]|null;}typeModelEnt......
  • 【JS】121-重温基础:流程控制和错误处理
    本文是 重温基础 系列文章的第二篇,需要让自己静下心来,学习,养成好习惯。本章节复习的是JS中的控制流语句,让我们能实现更多的交互功能。注意一点:在ES6之前,JS是没有块作用域......
  • 基于SqlSugar的开发框架循序渐进介绍(22)-- Vue3+TypeScript的前端工作流模块中实现统一
    在工作流页面中,除了特定的业务表单信息外,往往也需要同时展示通用申请单的相关信息,因此在页面设计的时候需要使用一些组件化的概念来实现动态的内容展示处理,本篇随笔介绍Vue3......
  • #10121. 「一本通 4.2 例 3」与众不同
    定义合法序列: 含有的元素各不相同查询[l,r]有多少合法序列 处理出stt[i]表示以i结尾的合法序列的起点stt[i]=stt[i-1],last[a[i]]+1stt[]单调增,可以找......
  • [Typescript] 119. Extreme - Get Readonly Keys
    Implementageneric GetReadonlyKeys<T> thatreturnsaunionofthereadonlykeysofanObject.ForexampleinterfaceTodo{readonlytitle:stringreado......
  • typescript笔记
    letu:undefined=undefinedletn:null=null类型注解functiongreeter(person:string){return'Hello,'+person}letuser=[0,1,2]console.log(gre......