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

210
Visualizações
How can I enforce a key to be of type string (and not number or symbol)?

I want to create a type for an object with string keys and arbitrary values. Thus, my desired type (let's call it Obj) should cause a type error for the following obj object because its key is of type number:

const obj: Obj = {
    1: "foo" // I'd expect a TS error here because the key 1 is of type `number` and not `string`
};

However, all of my attempts don't cause any TS error! Here are my three attempts:

  1. Built-in Record type (which implicitly uses the in operator):
type Obj = Record<'1', any>;

const obj: Obj = {
    1: "foo" // TS error expected! ❌
};
  1. Mapped type (to recreate Record type):
type Obj = { [key: string]: any };

const obj: Obj = {
    1: "foo" // TS error expected! ❌
};
  1. Custom Record type with key remapping via as:
type MyRecord<K extends string, T> = {
    [P in K as string]: T;
};
type Obj = MyRecord<string, any>;

const obj: Obj = {
    1: "foo" // TS error expected! ❌
};
  1. Even using a literal type '1' does not enforce the key to be '1', but allows the number 1:
type Obj = Record<'1', any>;

const obj: Obj = {
    1: "foo" // TS error expected! ❌
};

Is it in general possible, or is it due to some property of a JavaScript object that it's not possible?

TS Playground of my code.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The 1 is coerced to a string ("1") when defining an object literal. You cannot define an object with number keys:

const obj = {
  prop: 'value',
  1: 'another value',
};

for (const key in obj) {
  console.log(key, typeof key);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

By the keyof page of TypeScript doc

JavaScript object keys are always coerced to a string, so obj[0] is always the same as obj["0"]

So in fact {1: "foo"} is just as same as {"1": "foo"}.

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