Problem
interface Person {
name: string;
email: string;
}
function logRecord(record: Record<string, string>): void {
/* ... */
}
const me: Person = {
name: "John",
email: "john@doe.com",
};
logRecord(me); // error: "The argument of type 'Person' is not assignable to the parameter of type 'Record<string, string>'"
const dictionary: Record<string, string> = me; // error: "The argument of type 'Person' is not assignable to the parameter of type 'Record<string, string>'"
Solution #1: Use type
instead of interface
type Person = {
name: string;
email: string;
};
function logRecord(record: Record<string, string>): void {
/* ... */
}
const me: Person = {
name: "John",
email: "john@doe.com",
};
logRecord(me); // works
const dictionary: Record<string, string> = me; // works
Solution #2: Create a shadow copy with decomposition
interface Person {
name: string;
email: string;
}
function logRecord(record: Record<string, string>): void {
/* ... */
}
const me: Person = {
name: "John",
email: "john@doe.com",
};
logRecord({ ...me }); // works
const dictionary: Record<string, string> = { ...me }; // works
Solution #3: Use satisfies
to create variable
interface Person {
name: string;
email: string;
}
function logRecord(record: Record<string, string>): void {
/* ... */
}
const me = {
name: "John",
email: "john@doe.com",
} satisfies Person;
logRecord(me); // works
const dictionary: Record<string, string> = me; // works
Solution #4: Use as
keyword
interface Person {
name: string;
email: string;
}
function logRecord(record: Record<string, string>): void {
/* ... */
}
const me: Person = {
name: "John",
email: "john@doe.com",
};
logRecord(me as unknown as Record<string, string>); // works
const dictionary: Record<string, string> = me as unknown as Record<string, string>; // works
What to choose?
If you can, try to change interface into type. It is the best solution at this moment. Resolving the problem with a shadow copy has an impact on runtime execution code so is not recommended (if you do not need to create a shadow copy because of JavaScript, do not do it).