首页 > typescript中关于类型声明和命名空间的问题

typescript中关于类型声明和命名空间的问题

例如:

//Namespace.ts
export const Namespace = {
    B
    C
    D
}
class B{}
class C{}
class D{}
//main.ts
import {Namespace} from 'Namespace'
var b: Namespace.B;

这样会报错:

[ts] Cannot find namespace 'Namespace'.

正确的做法应该是怎么样的,是否必须得使用 typescript 的 namespace?


我找到了一个更好的办法
http://stackoverflow.com/questions/31983209/using-namespace-spread-over-multiple-files-in-typescript

// Classes/Animals.ts
export * from '.\Animals\Mammals';
export * from '.\Animals\Reptiles';
Then import the types from the new module as usual:

// app.ts
import * as Animals from '.\Classes\Animals'

let dog: Animals.Dog;
let snake: Animals.Snake;
Or

// app.ts
import { Dog, Snake } from '.\Classes\Animals'

let dog: Dog;
let snake: Snake;

你那么写确实有点问题:

export namespace Namespace {
    export class B {
        constructor() {
            console.log('hello');
        }
    }
}

main.ts

import { Namespace} from './script';

new Namespace.B();
【热门文章】
【热门文章】