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

98
Views
Type inference for constant object provided as function parameter accepting a mapped type

I came around a weird TypeScript behavior and was wondering how to get around it.

I have the following function, which accepts a single parameter opts.

type Opts<E extends string | number> = { [P in E]?: string };

function setOpts(opts: Opts<O>): void {
  // does stuff with <opts>
}

The idea of the Opts<O> type is that it creates a type that accepts an object with keys of type O and values of type string. For example, if I have a group of colors, say BLUE, RED and GREEN, I would get the following:

let colors: Opts<'BLUE'|'RED'|'GREEN'> = {
  BLUE: 'Blue color',
  RED: 'Red color',
  GREEN: 'Green color'
}

Now, up to here, everything works fine. When I call my function like bellow, I get a typescript error, since YELLOW is not an acceptable key. That is what I expect and what I want.

setOpts({ BLUE: 'b', RED: 'r', GREEN: 'g', YELLOW: 'y' })

However, for some reason (that I don't understand), when I define a constant with the exact same value as above and then pass that constant as a parameter of my function, typescript compiles without error. Obviously, I don't want that.

const param = { BLUE: 'b', RED: 'r', GREEN: 'g', YELLOW: 'y' };
setOpts(param);

Am I wrong to assume that typescript should infer a type for param. Is there a way I can force such a behavior?

about 4 years ago · Juan Pablo Isaza
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!