2016-07-28 93 views
0

我有一個命名空間的設置是這樣的:導入命名空間類專門

export namespace Entities { 

    export class Car { } 

    export class Motorbike { } 

} 

在另一大類,然後我導入CarMotorbike。但是,我無法簡潔地導入它們。如果我嘗試導入他們是這樣的:

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

我得到這個錯誤(在.Entities後):

'' 預期。

我能夠做到這一點:

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

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

但最好我將能夠將其導入,而無需手動導入的命名空間。這可能嗎?

回答

0

我不明白你爲什麼想在這種情況下使用命名空間。這裏有幾種選擇:

  • 在全局聲明文件中定義你的類。然後,您可以直接在程序中使用您的類型,而無需導入它們。

    // index.d.ts 
    declare class Car { 
        someProperty: any 
    } 
    
    // app.ts 
    let theCar: Car 
    theCar.someProperty 
    
  • 在模塊聲明文件中定義您的類。您將不得不導入要使用的特定類型。

    // index.d.ts 
    export class Car { 
        someProperty: any 
    } 
    
    // app.ts 
    import { Car } from './index' 
    let theCar: Car 
    theCar.someProperty