Type Challenges Judge

Binary to Decimal

提出詳細

type BitNum = [TNumOf<1>, TNumOf<2>, TNumOf<4>, TNumOf<8>, TNumOf<16>, TNumOf<32>, TNumOf<64>, TNumOf<128>]; type TNum = 0[]; type TNumOf<N extends number, _A extends 0[] = []> = _A["length"] extends N ? _A : TNumOf<N, [..._A, 0]>; type AddTNum<A extends TNum, B extends TNum> = [...A, ...B]; type PlusOne<N extends number> = [...TNumOf<N>, 0]["length"] extends number ? [...TNumOf<N>, 0]["length"] : never; type SplitStr<S extends string> = S extends `${infer L}${infer R}` ? [L, ...SplitStr<R>] : [] type Reverse<T extends unknown[], _Res extends unknown[] = []> = T extends [infer L, ...infer R] ? R extends unknown[] ? Reverse<R, [L, ..._Res]> : [L, ..._Res] : _Res type BinaryToDecimal_Impl<S extends string[], _Digit extends number = 0, _Res extends TNum = []> = _Digit extends S["length"] ? _Res["length"] : S[_Digit] extends "1" ? BinaryToDecimal_Impl<S, PlusOne<_Digit>, AddTNum<_Res, BitNum[_Digit]>> : BinaryToDecimal_Impl<S, PlusOne<_Digit>, _Res>; type BinaryToDecimal<S extends string> = BinaryToDecimal_Impl<Reverse<SplitStr<S>>>;
提出日時2025-01-19 17:03:35
問題Binary to Decimal
ユーザーookkoouu
ステータスAccepted
テストケース
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<BinaryToDecimal<'10'>, 2>>, Expect<Equal<BinaryToDecimal<'0011'>, 3>>, Expect<Equal<BinaryToDecimal<'00000000'>, 0>>, Expect<Equal<BinaryToDecimal<'11111111'>, 255>>, Expect<Equal<BinaryToDecimal<'10101010'>, 170>>, ]