• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

120
Vistas
How to convert custom interface to Record<string, unknown>?

Given the following sample code

interface MyInterface { foo: string; }

function doSomethingWith(x: Record<string, unknown>) { /* ... */ }

const myInterface: MyInterface = { foo: 'bar' };

doSomethingWith(myInterface);

It is not possible to pass the variable to the function because

Argument of type 'MyInterface' is not assignable to parameter of type 'Record<string, unknown>'. Index signature for type 'string' is missing in type 'MyInterface'.(2345)

I solved the problem by converting the variable

const myInterfaceAsRecord = (myInterface as unknown) as Record<string, unknown>;

and would like to know if there are any "better" ways to pass in the interface to the function?

almost 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just use type instead interface

type MyInterface = { foo: string; }

function doSomethingWith(x: Record<string, unknown>) { /* ... */ }

const myInterface: MyInterface = { foo: 'bar' };

doSomethingWith(myInterface);

See this answer for more context

almost 3 years ago · Juan Pablo Isaza Denunciar

0

One option would be to extend the interface from the record:

interface MyInterface extends Record<string, unknown> { foo: string; }

function doSomethingWith(x: Record<string, unknown>) { /* ... */ }

const myInterface: MyInterface = { foo: 'bar' };

doSomethingWith(myInterface);
almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda