What does the following declaration merge between a namespace and a class enable? class Animal { name: string = ”; } namespace Animal { export function create(name: string): Animal { const a = new Animal(); a.name = name; return a; } }

TypeScript Professional Hard

TypeScript Professional — Hard

What does the following declaration merge between a namespace and a class enable? class Animal { name: string = ”; } namespace Animal { export function create(name: string): Animal { const a = new Animal(); a.name = name; return a; } }

Key points

  • Namespace merging in TypeScript allows combining declarations of the same name
  • The namespace function create() acts as a factory method for the Animal class
  • This approach keeps the class definition clean while adding functionality
  • The namespace functions are accessible as if they were part of the class

Ready to go further?

Related questions