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

95 Views Asked by At

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?

0

There are 0 best solutions below