Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda