← Home

TypeScript Name Space Tricks

8 August, 2024

One interesting trick from @mattpocockuk in the way how namespace works in TypeScript.

When we export a namespace with the same name with a function, the declaration will be merged.

export declare namespace myFunc {
  type MyType = string
}

export function myFunc(n: number): string {
  return `${n}`
}

Then, in other file we can:

import { myFunc } from './lib/myFunc'

let x: myFunc.MyType = myFunc(1)
console.log(x)