Created
March 19, 2022 13:15
-
-
Save tosuke/f2a317f521a180788a773bc5778cbcce to your computer and use it in GitHub Desktop.
TypeScriptの型で巨大な数を扱う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 再帰消費数が 10*桁数 程度 | |
// 適当にテーブルを容易すれば定数倍の改善ができるので必要ならやる | |
type Repeat<T, N, Acc extends any[] = []> = Acc['length'] extends N ? Acc : Repeat<T, N, [T, ...Acc]> | |
type EncodeAux<D, Acc extends any[] = []> = `${Acc['length']}` extends D ? Acc['length'] : EncodeAux<D, [0, ...Acc]> | |
type Encode<T extends number | string> = | |
T extends number ? Encode<`${T}`> : | |
T extends `${infer D}${infer Rest}` ? [EncodeAux<D>, ...Encode<Rest>] : | |
[] | |
type Decode<T, A extends any[] = []> = | |
T extends [infer D, ...(infer Rest)] ? ( | |
Decode<Rest, [...A, ...A, ...A, ...A, ...A, ...A, ...A, ...A, ...A, ...A, ...Repeat<0, D>]> | |
) : | |
T extends [] ? A['length'] : | |
never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment