Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!