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

483
Views
Typescript: Generate types from an object

I just started learning Typescript and need some help with this example.


const alphabet = {
    'a': {lower: 97, upper: 65},
    'b': {lower: 98, upper: 66}
}

type Char = 'a' | 'b'

function printSome(char: Char){
    console.log(alphabet[char])
}

Instead of manually updating the Char type, I'd like to dynamically update generate types from the alphabet object.

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

0

You could use the keyof and typeof operators:

const alphabet = {
    'a': {lower: 97, upper: 65},
    'b': {lower: 98, upper: 66}
}

type Char = keyof typeof alphabet;

function printSome(char: Char){
    console.log(alphabet[char])
}

This basically turns alphabet into a type and then gets the keys of that type.

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!