Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Use interface as enum

How I usually do things is this:

enum tmp {
  a,
  b
}

const doStuff = (val: tmp) => {
  console.log(val);
}

doStuff(tmp.b); // logs 1

But a library I use is doing smth like this:

interface tmpMap {
  a: 0;
  b: 1;
}

const tmp: tmpMap;

So I tried doing this:

const doStuff = (val: tmpMap) => {
  console.log(val);
}

doStuff(tmp.b);

But this throws a bunch of errors, and just doesn't work.

Variable 'tmp' is used before being assigned.

Argument of type 'number' is not assignable to parameter of type 'tmpMap'.

Here's a link to playground.

Is there a way to use this setup the same way as the enum setup?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Playground

interface tmpMap {
  a: 0;
  b: 1;
}

// You'll have to actually assign the constant
declare const tmp: tmpMap;

const doStuff = (val: tmpMap[keyof tmpMap]) => {
  console.log(val);
}

doStuff(tmp.b);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!