UID1396355性别保密经验 EP铁粒 粒回帖0主题精华在线时间 小时注册时间2023-9-29最后登录1970-1-1
|
本期将单纯使用工具类型实现,无任何js代码(除了展示类型的除外)
代码量巨大,我就不搞解析了,完全基于本人对ts的理解写出来的原创代码,无任何借鉴,你说史山那就是史山
type number_string = ['0','1','2','3','4','5','6','7','8','9']
type number_ = [0,1,2,3,4,5,6,7,8,9]
type big = [10,11,12,13,14,15,16,17,18,19]
//移除元组开头元素
export type removeTop<A> = A extends [infer T,...infer data] ? [...data] : never
//结构对比
export type isOneType<t,T> = keyof t extends keyof T ? keyof T extends keyof t ? true : false : false
//全同对比
export type isOne<ty,yt> = ty extends yt ? yt extends ty ? true : false : false
//移除字符串开头的字符
type string_remove_top<s> = s extends `${infer _h}${infer data}` ? data : never
//获取字符串开头的字符
type get_string_top<s> = s extends `${infer _h}${infer ho}` ? _h :never
//字符串转数组
type string_to_array<s extends string,new_ extends []> = true extends isOne<s,''> ? new_ : string_to_array<string_remove_top<s>,[...new_,get_string_top<s>]>
export type StringToArray<s extends string> = string_to_array<s,[]>
//类型映射
export type type_to_else<t,contrast extends any[],to extends any[]> = true extends isOneType<t,contrast[0]> ? to[0] : type_to_else<t,removeTop<contrast>,removeTop<to>>
//获取元素索引
type indexof<target extends any[],type_ extends any,Counter extends []> = true extends isOne<target[0],type_> ? Counter['length'] : indexof<removeTop<target>,type_,[0,...Counter]>
export type indexOf<target,type_> = indexof<target,type_,[]>
//填充数组
export type fill_array<array extends any[],length extends number,element extends any[]> = true extends isOne<array['length'],length> ? array : fill_array<[element,...array],length,element>
//分割数组
type partition<array extends any[],length extends number,result extends [[],[...array]]> = true extends isOne<result[0]['length'],length> ? result : partition<removeTop<array>,length,[[...result[0],array[0]],[...removeTop<array>]]>
export type Partition<array extends any[],length extends number> = partition<array,length,[[],[...array]]>
//简单加法
type ad<a extends number,b extends number> = [...fill_array<[],a,0>,...fill_array<[],b,0>]['length']
//给字符串开头添加字符串
type string_add_top<s extends string,a extends string> = `${a}${s}`
//字符串转数字(9以内)
type string_to_number<s extends string> = indexOf<number_string,s>
//个位
type individual_position<n extends number> = indexOf<big,n>
//移除数组末尾
type array_remove_end<n extends any[]> = n extends [...infer h,infer e] ? h : []
//获取数组末尾
type get_array_end<n extends any[]> = n extends [...infer h,infer e] ? e : n
//判断元素在不在数组
type includes<array extends any[],element extends any> = true extends isOne<array,[]> ? false : true extends isOne<get_array_end<array>,element> ? true : includes<array_remove_end<array>,element>
//进位
type carry<n extends number[],after extends number[],> = true extends isOne<n,[]> ? after : true extends isOne<n['length'],1> ? [n[0],...after] : true extends includes<big,get_array_end<n>> ? carry<[...array_remove_end<array_remove_end<n>>,ad<get_array_end<array_remove_end<n>>,1>],[individual_position<get_array_end<n>>,...after]> : carry<array_remove_end<n>,[get_array_end<n>,...after]>
type Carry<n extends number[]> = carry<n,[]>
//拆解数字字符串
type partition_string<s extends string,new_ extends []> = true extends isOne<s,''> ? new_ : partition_string<string_remove_top<s>,[...new_,string_to_number<get_string_top<s>>]>
type PartitionString<s extends string> = partition_string<s,[]>
//默认值
type default_value<n,de> = true extends isOne<n,never> ? de : true extends isOne<n,undefined> ? de : true extends isOne<n,[]> ? de : n
//数组相加-末起
type array_number_add<a extends number[],b extends number[],c extends []> = true extends isOne<a[0],undefined> ? true extends isOne<b[0],undefined> ? c : array_number_add<array_remove_end<a>,array_remove_end<b>,[ad<default_value<get_array_end<a>,0>,default_value<get_array_end<b>,0>>,...c]> :array_number_add<array_remove_end<a>,array_remove_end<b>,[ad<default_value<get_array_end<a>,0>,default_value<get_array_end<b>,0>>,...c]>
type ArrayNumberAdd< a extends number[],b extends number[]> = array_number_add<a,b,[]>
//数字字符串数组转数字数组
type string_number_array_to_number_array<a extends string,result extends []> = true extends isOne<a,''> ? result : string_number_array_to_number_array<string_remove_top<a>,[...result,string_to_number<get_string_top<a>>]>
type StringArrayToNumberArray<a extends string> = string_number_array_to_number_array<a,[]>
display<string_number_array_to_number_array<'2738',[]>>()
//数组转字符串
type array_to_string<array extends any[],s extends string> = true extends isOne<array,[]> ? s : array_to_string<removeTop<array>,`${s}${array[0]}`>
type add<a extends string,b extends string> = array_to_string<Carry<ArrayNumberAdd<StringArrayToNumberArray<a>,StringArrayToNumberArray<b>>>,''>
使用
function display<T>():T
|
|