开启辅助访问     
收藏本站

站内搜索

搜索

Minecraft(我的世界)苦力怕论坛

[其他教程] 使用typescript类型系统实现正整数加法运算

 发表于 2024-8-21 23:57:57 来自手机|显示全部楼层|阅读模式 IP:安徽省
本期将单纯使用工具类型实现,无任何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
Image_1724255785164.jpg
苦力怕论坛,感谢有您~
 发表于 2024-8-25 14:50:36 来自手机|显示全部楼层 IP:天津
你的ts基本功是我一辈子无法比拟的
2#2024-8-25 14:50:36回复收起回复
苦力怕论坛,感谢有您~
回复支持

使用道具举报

 发表于 2024-9-11 21:30:54|显示全部楼层 IP:四川省
ts体操一般难度,基本就是const数组特性


  1. type MetaNum = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
  2. // 分割
  3. type Split<
  4.   T extends string,
  5.   R extends boolean = false,
  6.   _R extends Array<string> = [],
  7. > = T extends `${infer X}${infer Y}` ? (R extends true ? Split<Y, R, [X, ..._R]> : Split<Y, R, [..._R, X]>) : _R
  8. // 联合
  9. type Join<T extends Array<string>, R extends boolean = false, _R extends string = ''> = T extends [
  10.   infer A extends string,
  11.   ...infer B extends Array<string>,
  12. ]
  13.   ? Join<B, R, R extends true ? `${A}${_R}` : `${_R}${A}`>
  14.   : _R
  15. // 获取索引
  16. type ResolveIdx<T extends string, R extends Array<string> = MetaNum> = R extends [infer X, ...infer Y]
  17.   ? T extends X
  18.     ? R extends [infer _, ...infer Rest]
  19.       ? Rest['length']
  20.       : never
  21.     : ResolveIdx<T, Extract<Y, Array<string>>>
  22.   : never
  23. // 创建空白索引数组
  24. type GenBlankArr<T extends number, R extends Array<number> = []> = R['length'] extends T
  25.   ? R
  26.   : GenBlankArr<T, [...R, R['length']]>
  27. // 原子加发
  28. type MetaAdd<A extends string, B extends string> = Extract<
  29.   [...GenBlankArr<ResolveIdx<A>>, ...GenBlankArr<ResolveIdx<B>>]['length'],
  30.   number
  31. >
  32. // 获取进位和余位
  33. type ResolveNum<T extends number> = `${T}` extends `${infer X}${infer Y}` ? (Y extends '' ? ['0', X] : [X, Y]) : never
  34. // 递推进位值计算
  35. type ReduceNum<
  36.   A extends string,
  37.   B extends string,
  38.   R extends Array<number> = [],
  39.   SplitA = Split<A, true>,
  40.   SplitB = Split<B, true>,
  41. > = SplitA extends [infer XA extends string, ...infer YA extends Array<string>]
  42.   ? SplitB extends [infer XB extends string, ...infer YB extends Array<string>]
  43.     ? ReduceNum<Join<YA, true>, Join<YB, true>, [...R, MetaAdd<XA, XB>]>
  44.     : ReduceNum<Join<YA, true>, '', [...R, MetaAdd<XA, '0'>]>
  45.   : SplitB extends [infer XB extends string, ...infer YB extends Array<string>]
  46.   ? ReduceNum<'', Join<YB, true>, [...R, MetaAdd<'0', XB>]>
  47.   : R
  48. // 地推进位
  49. type Tee<T extends Array<number>, R extends string = '', CR extends string = '0'> = T extends [
  50.   infer A extends number,
  51.   ...infer B extends Array<number>,
  52. ]
  53.   ? ResolveNum<A> extends [infer X extends string, infer Y extends string]
  54.     ? ResolveNum<MetaAdd<Y, CR>> extends [infer _X extends string, infer _Y extends string]
  55.       ? Tee<B, `${_Y}${R}`, `${MetaAdd<X, _X>}`>
  56.       : never
  57.     : never
  58.   : CR extends '0'
  59.   ? R
  60.   : `${CR}${R}`
  61. // 判断是否合法
  62. type IsValid<
  63.   T extends string,
  64.   Temp extends Array<string> = [],
  65.   SplitT extends Array<string> = Split<T>,
  66. > = SplitT extends [infer X, ...infer Y extends Array<string>]
  67.   ? X extends MetaNum[number]
  68.     ? X extends '0'
  69.       ? Temp['length'] extends 0
  70.         ? SplitT['length'] extends 1
  71.           ? true
  72.           : false
  73.         : IsValid<Join<Y>, [...Temp, X]>
  74.       : IsValid<Join<Y>, [...Temp, X]>
  75.     : false
  76.   : Temp['length'] extends 0
  77.   ? false
  78.   : true
  79. // 组合加法
  80. type Add<A extends string, B extends string, IsValidA = IsValid<A>, IsValidB = IsValid<B>> = IsValidA extends IsValidB
  81.   ? IsValidA extends true
  82.     ? // @ts-ignore
  83.       Tee<ReduceNum<A, B>>
  84.     : never
  85.   : never

  86. type Foo_1 = Add<'0', '1'>
  87. type Foo_1_1 = Add<'0', ''>
  88. type Foo_2 = Add<'1', '0'>
  89. type Foo_3 = Add<'0', '0'>
  90. type Foo_4 = Add<'123', '456'>
  91. type Foo_5 = Add<'937', '9999999999999878'>
  92. type Foo_6 = Add<'12846026972358281685236841339743503218625460074', '16738438016761768034928435260197280468667948046'>
复制代码
3#2024-9-11 21:30:54收起回复
夙S溯2024-9-11 23:38IP:安徽省
回复举报
你接下来是不是还要再跑到我写乘法的帖子里发一遍👿
苦力怕论坛,感谢有您~
回复支持

使用道具举报

本版积分规则

本站
关于我们
联系我们
坛史纲要
官方
哔哩哔哩
技术博客
下载
网易版
安卓版
JAVA
反馈
意见建议
教程中心
更多
捐助本站
QQ群
QQ群

QQ群

访问手机版

访问手机版

手机版|小黑屋|系统状态|klpbbs.com

粤公网安备 44200002445329号 | 由 木韩网络 提供支持 | GMT+8, 2024-11-24 10:55

声明:本站与Mojang以及微软公司没有从属关系

Powered by Discuz! X3.4 粤ICP备2023071842号-3