Given the following example:
interface Foo {
name: string;
}
interface Bar extends Foo {
displayName: string;
}
const foo: Foo = { name: "foo" };
const bar: Bar = { name: "bar", displayName: "dn" };
const getName = <T extends Foo>(obj: T = foo): string => {
return obj.name;
};
getName(foo); // Ok
getName(bar); // Ok
obj: T = foo causes an error. Even though, T extends Foo, this is not accepted. Why is this the case and what would be an workaround?