Consider the following generic function in TypeScript:
interface Foo { id: string; }
const foo = <Foo>{id: 'bar'};
function baz<T>(x: T) { console.log(x.id); }
baz(foo);
This doesn't compile:
test.ts(6,17): error TS2339: Property 'id' does not exist on type 'T'.
It seems to me that TypeScript ought to be able to deal with this situation. If it can't, then I have to do something like this:
interface IHasId { id: string; }
interface Foo extends IHasId { }
const foo = <Foo>{id: 'bar'};
function baz<T>(x: IHasId) { console.log(x.id); }
baz<Foo>(foo);
Is there any alternative or more idiomatic way to deal with this situation?
anyinstead?