Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda