0

I have a namespace set up like this:

export namespace Entities {

    export class Car { }

    export class Motorbike { }

}

In another class, I then import Car and Motorbike. However, I am unable to import them succinctly. If I try to import them like this:

import { Entities.Car as Car, Entities.Motorbike as Motorbike } from "./somefile.ts";

I get this error (on the . after Entities):

',' expected.

I am able to do this:

// import whole namespace
import { Entities } from "./somefile.ts";

// use the type directly:
let vehicle: Entities.Car;

However ideally I would be able to import them without manually importing the namespace. Is this possible?

James Monger
  • 10,181
  • 7
  • 62
  • 98

1 Answers1

0

I don't see why you would want to use a namespace in this situation. You have several options here:

  • Define your classes in a global declaration file. You can then use your types directly in your program, without having to import them.

    // index.d.ts
    declare class Car {
      someProperty: any
    }
    
    // app.ts
    let theCar: Car
    theCar.someProperty
    
  • Define your classes in a module declaration file. You'll have to then import the specific types you want to use.

    // index.d.ts
    export class Car {
      someProperty: any
    }
    
    // app.ts
    import { Car } from './index'
    let theCar: Car
    theCar.someProperty
    
Pelle Jacobs
  • 2,379
  • 1
  • 21
  • 25