I need to add functionality to existing Node.js class, specifically
Server
import * as net from 'net'
type ShutdownableServer = net.Server & { shutdown(): Promise<void> }
function withShutdown(server: net.Server): ShutdownableServer {
/* ... */
}
Is it possible to use something along the lines of the following snippet?
Yes.
type ShutdownableServer = net.Server & { shutdown(): Promise<void> }
function withShutdown(server: net.Server): ShutdownableServer {
const result = server as ShutdownableServer;
result.shutdown = /*your code*/;
return result;
}
I used an assertion. Docs https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html