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

188
Visualizações
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?

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

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

about 4 years ago · Juan Pablo Isaza Relatório

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);
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