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

426
Views
How to create a template literal type from object to have keys=values?

I have an object

{
  foo: "bar",
  a: "b",
  roses: "red",
}

I would like to create a template literal which would give me the following possibilities:

"foo=bar" | "a=b" | "roses=red" 

So basically, key=value

What I've done so far is:

type Foo = `${keyof typeof obj}=${typeof obj[keyof typeof obj]}`;

But this give me every possible combination:

"foo=bar" | "foo=b" | "foo=red" | "a=bar" | ...

I feel I need to use a generic but I'm not able to make it work

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

0

You can use a self-indexing mapped type:

const obj = {
  foo: "bar",
  a: "b",
  roses: "red",
} as const;

type Foo = {
    [K in keyof typeof obj]: `${K}=${typeof obj[K]}`
}[keyof typeof obj];

This evaluates to:

type Foo = "foo=bar" | "a=b" | "roses=red"

Playground link

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!