File tree 2 files changed +21
-0
lines changed
type-challenges/6141-hard-binary-to-decimal
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ ] ;
You can’t perform that action at this time.
0 commit comments