当前位置:网站首页>Wonderful use of TS type gymnastics string
Wonderful use of TS type gymnastics string
2022-07-06 07:29:00 【Jioho_】
TS Type gymnastics And Wonderful use of string
Here is a record of some ingenious string processing skills encountered in the process of doing questions , The main topic introduced later is 00529-medium-absolute
For the use of strings , The chapter of pre knowledge has been introduced , Examples are as follows . After understanding this part of the examples, I believe that many problems can be solved
type testInfer<T extends string> = T extends `${
infer F}${
infer S}${
infer R}` ? [F, S, R] : T
type testInfer1 = testInfer<'123456'>
// According to the characteristics of placeholders , front F and S To occupy separately 2 Characters , Give the rest R Occupied
// type testInfer1 = ["1", "2", "3456"]
// Make a little change , stay S Add a... After the placeholder 5
type testInfer2<T extends string> = T extends `${
infer F}${
infer S}5${
infer R}` ? [F, S, R] : T
type testInfer3 = testInfer<'123456'>
// F Occupy the first character = 1
// S occupy 2-4, Because in R There was one before 5, therefore S Represents the beginning of the second character to 5 All characters of
// that R It's from 5 Start , To the end , So the results are as follows :
// type testInfer1 = ["1", "234", "6"]
00529-medium-absolute Topic explanation
demand : Implement a receive string,number or bigInt Of type parameter Absolute type , Returns a positive string .
- receive string,number, perhaps bigInt
- Returns a positive integer
- Return string
Pick a few representative test cases
type testcase = Absolute<0>
type testcase = Absolute<-5>
type testcase = Absolute<'0'>
type testcase = Absolute<'-0'>
type testcase = Absolute<'10'>
type testcase = Absolute<-1_000_000n>
type testcase = Absolute<9_999n>
Their thinking
Ideas 1: A more circuitous idea
- First convert numbers into strings
- Compare string by string ( Use infer Extract each string , Then compare , After the comparison is successful, it is stored in a variable R in )
- When the incoming string is empty, the variable is returned R
Ideas 1 Realization :
type NumberStr = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type Absolute<T extends number | string | bigint, R extends string = ''> = `${
T}` extends `${
infer F}${
infer Rest}`
? F extends NumberStr
? Absolute<Rest, `${
R}${
F}`>
: Absolute<Rest, R>
: R
among , When the incoming generic T May be number perhaps bigInt When it comes to type , It can't be used directly extends xxx
So the first trick is ${T}
Whether it is string type or not , All converted to strings ( This is also because numeric types can be converted to strings )
The rest is to compare character by character .
Ideas 2: Use digital features , such as 1_000_000n In procedure tostring In fact, it will be automatically converted to 1000000, We don't need to consider too much removal \_
perhaps Remove n
The logic of
Ideas 2 Realization :
type Absolute<T extends number | string | bigint> = `${
T}` extends `-${
infer Num}` ? Num : `${
T}`
This method directly matches whether the incoming content has -
Number
- If not , Description is a positive integer ( It could be numbers ) So wrap a layer `` Output directly as a string
- Yes
-
No , utilize infer and Properties of strings , Rule out the first -, The rest is the string corresponding to the number
Summary
- In the process of matching , have access to
${T}
The grammar of , hold number Also converted to string - Numeric type , Include bigInt In fact, the type conversion will be automatically converted to numbers , Using this feature, you can write less code
边栏推荐
- Google可能在春节后回归中国市场。
- Get/post/put/patch/delete meaning
- CF1036C Classy Numbers 题解
- How to delete all the words before or after a symbol in word
- GET/POST/PUT/PATCH/DELETE含义
- The differences and advantages and disadvantages between cookies, seeion and token
- Force buckle day31
- Typescript interface properties
- TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
- Scala语言学习-08-抽象类
猜你喜欢
1091: two or three things in childhood (multi instance test)
杰理之AD 系列 MIDI 功能说明【篇】
烧录场景下的源代码防泄密方案分享
杰理之蓝牙设备想要发送数据给手机,需要手机先打开 notify 通道【篇】
ORACLE列转行--某字段按指定分隔符转多行
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
Oracle column to row -- a field is converted to multiple rows according to the specified separator
剪映的相关介绍
(4) Web security | penetration testing | network security web site source code and related analysis
QT color is converted to string and uint
随机推荐
QT color is converted to string and uint
Scala语言学习-08-抽象类
学go之路(一)go的基本介绍到第一个helloworld
leecode-C語言實現-15. 三數之和------思路待改進版
OpenJudge NOI 2.1 1661:Bomb Game
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
js对象获取属性的方法(.和[]方式)
超级浏览器是指纹浏览器吗?怎样选择一款好的超级浏览器?
chrome查看页面fps
(4) Web security | penetration testing | network security web site source code and related analysis
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer
[MySQL learning notes 30] lock (non tutorial)
Typescript indexable type
Seriously recommend several machine learning official account
杰理之普通透传测试---做数传搭配 APP 通信【篇】
On the world of NDK (2)
Multithreading and concurrent programming (2)
C - Inheritance - polymorphism - virtual function member (lower)
Summary of Digital IC design written examination questions (I)
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps