3

In TypeScript, why do I get an error when I assign an interface to an interface, but there is no error when I assign a type to an interface?

I want to know why this happens!

// ----------- Scenes one------------
interface A {
    [key: string]: string;
 };

interface B {
    name: string;
}

let b: B = { name: "pig" };
let a: A = b; //error      The type "B" cannot be assigned to the type "A".  The index signature is missing in type "B".

// ----------- Scenes two------------
interface A {
    [key: string]: string;
 };

type B = {
    name: string;
}

let b: B = { name: "pig" };
let a: A = b; //success ??

0 Answers0