ccnokes ccnokes - 1 year ago 86
TypeScript Question

How do I type a function object with additional functions attached as properties?

In TypeScript, I'd like to create a module that exports a function that has additional functions added to it, like so:

export default function log(msg: string) {

//Property 'warn' does not exist on type '(msg: string) => void'.
log.warn = function(msg: string) {

The usage of this could look like:


How do I tell TypeScript that my function object has additional properties on it so that it doesn't throw
Property 'warn' does not exist on type '(msg: string) => void'.

Answer Source

You would write it this way:

function log(msg: string) {

namespace log {
  export function warn(omen: string) { }

export default log;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download