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

238
Vistas
Typescript: Defining an interface with both dynamic and static keys

Im working with data that looks like

{
 8533864186048: "4",
 8533864218816: "1",
 isExchange: true,
 returnType: "exchange",
}

where the first two keys are dynamic but will always be strings

so I tried to define the interface as such:

interface ReturnData {
  [key: string]: string; <- Applying this to whole interface for some reason
  isExchange?: boolean;
  returnType?: ReturnType;
  selectedVariant: Variant;
}

So am having the issue where trying to define a dynamic member on the interface causes ts to try and assign all the other members to type string

What would be the correct way to go about this?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You could do something like:

interface ReturnData {
  isExchange?: boolean;
  returnType?: ReturnType;
  selectedVariant: Variant;
  [key: string]: string | boolean | ReturnType | Variant;
}

But instead of mixing dynamic and static property together, I think you should refactor your interface like this:

interface ReturnData {
  isExchange?: boolean;
  returnType?: ReturnType;
  selectedVariant: Variant;
  someProps: {
    [key: string]: string;
  }
}

so the interface is more readable.

over 4 years ago · Santiago Trujillo Denunciar

0

You can just restrict all number keys to be a string: [key:number]: string

type CustomReturnType = string;
type Variant = string;

interface ReturnData {
  [key: number]: string; // <--- any number key should be a string
  isExchange?: boolean;
  returnType?: CustomReturnType;
  selectedVariant: Variant;
}

const data: ReturnData = {
  isExchange: true,
  returnType: 'hello',
  selectedVariant: 'variant',
  2334234234: 'sd',
  234234234:23 // expected error
}
over 4 years ago · Santiago Trujillo Denunciar

0

You can define your interface like this

interface ReturnData {
    isExchange?: boolean;
    returnType?: ReturnType;
    selectedVariant: Variant;
    [key: string]: string | boolean | ReturnType | Variant;
}

Another approach is this if you only want numbered keys to be a string

type ReturnData = {
    [key in string | number]: key extends number ? string : string | boolean | ReturnType | Variant;
} & {
    isExchange?: boolean;
    returnType?: RT;
    selectedVariant: V;
};

const k: ReturnData = {
    8533864186048: "4",
    8533864186049: "3",
    8533864186048: true // invalid
};

Final Implementation will look like this

type ValidKeys = "isExchange" | "returnType" | "selectedVariant";

type ReturnData = {
    [key in string | number]: key extends ValidKeys ? string | boolean | ReturnType | Variant : string;
} & {
    isExchange?: boolean;
    returnType?: ReturnType;
    selectedVariant: Variant;
};
over 4 years ago · Santiago Trujillo 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