I'm looking to expand a data object in TypeScript by introducing new fields. Although I know it's a common practice in JavaScript, I'm struggling to get it to compile without making bar
optional in the provided snippet.
I am interested in finding a way to avoid having to make bar
optional. Any suggestions would be greatly appreciated. Thank you.
interface BaseDataObject {
foo: string;
}
interface ExtendedDataObject extends BaseDataObject {
bar?: string;
}
function extendData(input : BaseDataObject) : ExtendedDataObject {
var output : ExtendedDataObject = input;
output.bar = input.foo + ' some suffix';
return output;
}