1

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)

Yilmaz
  • 35,338
  • 10
  • 157
  • 202

2 Answers2

0

From running your code on the typescript playground, the only error I see is that Lookup is not defined. Considering that you are declaring an object, if you want to assign an interface to it (like Lookup), you'll have to mention each and every property in the interface. Trust me, you do not want to do this.

Regarding the question at hand, i am not able to understand the approach you are trying to take. The result can be achieved using something way simpler.

const binary = parseInt("AB5",16).toString(2)
console.log(binary)

Example


The following answers can be taken for reference:

Yash Kumar Verma
  • 9,427
  • 2
  • 17
  • 28
0

Declare lookup as a Record<string, string> (object mappings strings to strings) instead to avoid key errors:

const lookup: Record<string, string> = {
    // lookup goes here
};

// rest of code

Playground link

Aplet123
  • 33,825
  • 1
  • 29
  • 55