When I have an object of known type and want to add some properties to it. And I'm also too lazy to write an additional interface.
How can I get both inherited and infered types for properties on the same object?
For example, express adds properties to the request object and I want to do the same
import * as http from 'http';
/**
* @param {http.IncomingMessage} request
* @param {http.ServerResponse} response
*/
function doStuff(request, response) {
request.numericProp = 10;
console.log(request.numericProp + 20);
}
Here I see errors at .numericProp
Property 'numericProp' does not exist on type 'IncomingMessage'.ts(2339)
Of course, I can allow any additional props by doing this:
/**
* @param {http.IncomingMessage & { [key: string]: any }} request
* @param {http.ServerResponse} response
*/
And there will be no more errors while I still can see hints about IncomingMessage properties, BUT.. numericProp is any.
I want it to be automatically infered as number.
Is there any built-in trick, like Partial<>, or what kind of construction can I use?