this function is what hex-to-binary package uses. since it does not have types file and even declaring in decs.t.ts does not work, i tried to implement in typescript:
const lookup: Lookup = {
"0": "0000",
"1": "0001",
"2": "0010",
"3": "0011",
"4": "0100",
"5": "0101",
"6": "0110",
"7": "0111",
"8": "1000",
"9": "1001",
a: "1010",
b: "1011",
c: "1100",
d: "1101",
e: "1110",
f: "1111",
A: "1010",
B: "1011",
C: "1100",
D: "1101",
E: "1110",
F: "1111",
};
export function hexToBinary(s:string) {
let ret = "";
for (let i = 0, len = s.length; i < len; i++) {
ret += lookup[s[i]];
}
return ret;
}
so we pass a string to hexToBinary(s:string). Specifically this string has to have only binary characters since I could not figure out how to define it, I pass s:string
because s.length
will be defined.
lookup[s[i]]
is giving this error: "Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ 0: string; 1: string; 2: string; 3: string; 4: string; 5: string; 6: string; 7: string; 8: string; 9: string; a: string; b: string; c: string; d: string; e: string; f: string; A: string; B: string; C: string; D: string; E: string; F: string; }'.
No index signature with a parameter of type 'string' was found on type '{ 0: string; 1: string; 2: string; 3: string; 4: string; 5: string; 6: string; 7: string; 8: string; 9: string; a: string; b: string; c: string; d: string; e: string; f: string; A: string; B: string; C: string; D: string; E: string; F: string; }'"
So typescript is recognizing "lookup" as array. It does not understand that it is a computed property.
I defined an interface for lookup object:
interface Lookup {
"0"?: string;
"1"?: string;
"2"?: string;
"3"?: string;
"4"?: string;
"5"?: string;
"6"?: string;
"7"?: string;
"8"?: string;
"9"?: string;
a?: string;
b?: string;
c?: string;
d?: string;
e?: string;
f?: string;
A?: string;
B?: string;
C?: string;
D?: string;
E?: string;
F?: string;
}
But this time i got this error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Lookup'. No index signature with a parameter of type 'string' was found on type 'Lookup'.ts(7053)