Given the code below,
function system(): ISavable & ISerializable {
return {
num: 1, // error!
save() {},
load() {},
serialize() {},
deserialize() {},
}
}
interface ISavable {
save: () => void
load: () => void
}
interface ISerializable {
seriailze: () => void
deserialize: () => void
}
The problem
When returning property num: 1, TypeScript complains it is not specified by either ISavable or ISerializable—and that's expected.
One possible solution would be creating one extra type that exposes all the stuff this function is going to return—but I'm looking for something that allows me to go implicit when it comes to the function's unique properties.
To make myself even clearer, when you don't specify the return of your function, the type of it is inferred and you get autocomplete, etc. I want that plus to be able to infer the types and guarantee some explicitly specified properties are returned as well.
Question
I'd like to type the return of my function in a way where it requires the function to return the properties from both interfaces (ISavable and ISerializable), but still allows me to return extra properties freely, that may be unique to this function.
That said,
- Is it possible?
- If so, how?
I think my needs can be achieved through classes, but I'm pursuing a solution specifically for functions.