0

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?

sharkdarras
  • 21
  • 1
  • 1
  • `function setOpts(opts: Opts): void {` Where does the `O` come from? It's not defined anywhere. Did you forget a generic type parameter? – T.J. Crowder Mar 25 '22 at 16:09
  • [Excess property checks](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks) only apply to literals. In general, an object is allowed to be a subtype (a type with more stuff on it). And there [currently isn't any way to prevent that](https://stackoverflow.com/a/66467993/157247). – T.J. Crowder Mar 25 '22 at 16:12

0 Answers0