Why does this work with type alias, but not with an interface

Viewed 79

A colleague today came to me with the following situation:

He has an interface, and an object implementing that interface like so:

interface MyInterface {
    a: number;
    b: number
}

const myObject: MyInterface = {
    a:1,b:2
}

Additionally, he imports a function from a third party module, with accepts an argument of Type Record<string,unkonwn> and tries to pass the object to that function like so:

function foreignFunction(record: Record<string,unknown>){}

foreignFunction(myObject);

But TypeScript does not allow this. I then found out that it works, when instead of an interface a type alias is used like this:

type MyInterface = {
    a: number;
    b: number;
}

Can you explain to me, why it behaves like this?

0 Answers
Related