Suppose I have a TypeScript interface in a module, and also a function that return an initialized object of that interface:
export default interface Foo {
name: string;
surname: string;
cars: number;
};
export function newFoo(): foo {
return <Foo>{
name: '',
surname: '',
cars: 0
};
};
The problem is that I'm typing all the fields twice, and it's not only boring, but also error-prone.
I can't really use return <Foo>{}; because there are cases of objects being members of other objects, so I'll have missing properties and undefined pops out.
I could check if the property exists when calling it, but since the property doesn't have the ?, it means it's mandatory, so it's supposed to be there.
So, is there a better way to write the code above?