typescript 给变量赋值namespace里面的枚举值报错

// index.ts

/// <reference path="./enum.ts" />

const a = common.Country.CN;
// enum.ts
namespace common {
    export enum Country {
        CN = 0,
        US = 1,
    }
}

用ts-loader编译后总报错:

Uncaught ReferenceError: common is not defined

求解,为什么总是报未定义啊? 我头部已经声明了enum.ts了啊

阅读 6.3k
1 个回答

你需要把 namespace 也 export出去。这样:

// enum.ts
export namespace common {
    export enum Country {
        CN = 0,
        US = 1,
    }
}

使用的时候:

// index.ts
import { common } from 'enum.ts文件的相对路径/enum'

const a = common.Country.CN
推荐问题