Skip to content

Commit c3885e0

Browse files
committed
feat: add 6141-hard-binary-to-decimal
1 parent fb1728a commit c3885e0

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 参考 https://github.com/type-challenges/type-challenges/issues/6985
2+
// 我不理解
3+
type ExpandTwo<T extends any[]> = [...T, ...T];
4+
5+
type BinaryToDecimal<
6+
S extends string,
7+
R extends any[] = []
8+
> = S extends `${infer L}${infer Rest}`
9+
? L extends "0"
10+
? BinaryToDecimal<Rest, ExpandTwo<R>>
11+
: BinaryToDecimal<Rest, [...ExpandTwo<R>, 0]>
12+
: R["length"];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Equal, Expect } from "@type-challenges/utils";
2+
3+
type cases = [
4+
Expect<Equal<BinaryToDecimal<"10">, 2>>,
5+
Expect<Equal<BinaryToDecimal<"0011">, 3>>,
6+
Expect<Equal<BinaryToDecimal<"00000000">, 0>>,
7+
Expect<Equal<BinaryToDecimal<"11111111">, 255>>,
8+
Expect<Equal<BinaryToDecimal<"10101010">, 170>>
9+
];

0 commit comments

Comments
 (0)