I have an Interface like:
export interface Person {
name: string;
isMillenial: boolean;
}
And a function that outputs that interface like:
function makePerson(name: string, birthYear: number): Person {
let newPerson = {
name
}
// Calculate various properties, slowly building up the `newPerson` object
newPerson.isMillenial = birthYear < 1981
return newPerson
}
However this will fail with an error like:
Property 'isMillenial' is missing in type '{ name: string }' but required in type 'Person '.
I know I could declare all the properties of newPerson when I create it, to match what's expected - but I'd like to build the object I'm creating incrementally, adding properties until the object is complete. Is there a way I can build the object incrementally in TypeScript while still satisfying the type checking?
(If the answer is 'no, you need defaults', that's an acceptable answer)