0

I'm trying to achieve the following: (Never mind that the types are structured the same. This is for demo purpose only). The goal is to not have a single large file for the namespace I'd like to export.

import Animal from 'types/animal/index.ts'

const dog:Animal.Dog = {
  sound: 'bark',
  legs: 4
}

const cat:Animal.Cat = {
  sound: 'meow',
  legs: 4
}

types/animal/index.ts

// somehow import the animal types from separate files and export in one "Animal" namespace

types/Cat.ts

export namespace Animal {
  export interface Cat {
    sound: string
    legs: number
  }
}

types/Dog.ts

export namespace Animal {
  export interface Dog {
    sound: string
    legs: number
  }
}
micahblu
  • 4,924
  • 5
  • 27
  • 33
  • 2
    Have you read this? https://stackoverflow.com/questions/30357634/how-do-i-use-namespaces-with-typescript-external-modules – Ryan Cavanaugh Jun 15 '17 at 23:01

1 Answers1

1

Assuming that your Dog.ts and Cat.ts indeed reside in types, and not in types/animal, something like this in types/animal/index.ts should do the job:

import * as Animal_Dog from '../Dog';
import * as Animal_Cat from '../Cat';


namespace Animal {
    export type Dog = Animal_Dog.Animal.Dog;
    export type Cat = Animal_Cat.Animal.Cat;
}

export default Animal;
artem
  • 46,476
  • 8
  • 74
  • 78