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

170
Visualizações
How do I write out many variables in a type in short hand, from a JSON response, in typescript

I have a relatively trivial question, say I need to create a new type from a JSON Response

My JSON response (lets call it Label Response) is as follows

{
id1: "someString"
someField: "someString"
someMoreFields: "someString"
EvenMorefields: "someString"
EvenMoreMoreFields: "someString"
SoManyManyFields: "someString"
EndlessFields: "someString"
}

Now if I choose to create a type from this response it would be as follows (correct me if i am wrong)

export type LabelResponse =
      {
       Fields: string;
       FieldsMore:string;
       MoreMoreFields:string;
       MoreMoreMoreFields:string;
      }

This is very verbose Since the more IDs, I have the more fields I will need and all the Ids are of the same type.

Is there a way to possible write this in short hand, for example, in my function parameter, instead of declaring a type for it

For example instead of

function myFunction(label:LabelResponse)

Would it be possible to declare my type in the parameter brackets ? Like this:

function myFunction(label:DeclareMyTypeHereSomehow?)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can create a Record type include a string key and a value key like so:

type LabelResponse = Record<string, string>

const data: LabelResponse = {
    id1: "someString",
    id2: "someString",
    someRandomString: "someString",
    someWeirdString: "someString",
}

Playground

about 4 years ago · Juan Pablo Isaza Relatório

0

You can shorten the type definition using template literal types like following:

export type LabelResponse = { [K in `id${1|2|3|4}`]: string; }

This will give a type as:

type LabelResponse = {
    id1: string;
    id2: string;
    id3: string;
    id4: string;
}

Playground.


Or if you don't care about the actual range, you can use id${number}:

export type LabelResponse = { [K in `id${number}`]: string; }

// errors since key doesn't start with id
const x:  LabelResponse = {
  'x1': 'hello'
}

// works
const y: LabelResponse = {
  'id2': 'world'
}

// not works since it expects a number after id
const z: LabelResponse = {
  'idx': '!'
}

Playground


If you don't care about the key at all, then you can just do:

type LabelResponse = {
  [K: string]: string
}
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