Utilizing Object.assign
makes this task achievable.
type Log = {
(...args: any): void;
info: (...args: any) => void;
};
const log: Log = Object.assign((...args: any) => console.log(...args), {
info: (...args: any) => console.log('INFO:', ...args),
});
log('hello');
log.info('world');
Here is the compiled version:
"use strict";
const log = Object.assign((...args) => console.log(...args), {
info: (...args) => console.log('INFO:', ...args),
});
log('hello');
log.info('world');
If you prefer, there's no need to explicitly define the Log
type. TypeScript will deduce the types of objects passed to Object.assign
and create an intersection between them automatically.
const log = Object.assign((...args: any) => console.log(...args), {
info: (...args: any) => console.log('INFO:', ...args),
});
log('hello');
log.info('world');