【发布时间】:2019-12-06 05:41:39
【问题描述】:
我正在尝试导入一个导出枚举类型的自定义模块。但是,当我尝试使用导出的枚举时,我收到此错误 Error: Cannot find module 'Runescape'
如果我不使用枚举,则代码有效,因此 import 语句有效,或者如果我将枚举移动到我的主 ts 文件中。我试过编译器选项“preserveConstEnums”
tsconfig.json
{
"compilerOptions": {
"resolveJsonModule": true,
"moduleResolution": "node",
"esModuleInterop": true,
"sourceMap": true,
"outDir": "out",
"target": "es6"
}
}
Runescape.ts
declare module 'Runescape' {
...
/**
* @description Enum of all Runescape skills
* @enum
* @default
*/
export enum SkillsEnum {
ATTACK = 'attack',
STRENGTH = 'strength',
DEFENSE = 'defense',
RANGED = 'ranged',
PRAYER = 'prayer',
MAGIC = 'magic',
RUNECRAFT = 'runecraft',
CONSTRUCTION = 'construction',
HITPOINTS = 'hitpoints',
AGILITY = 'agility',
HERBLORE = 'herblore',
THIEVING = 'thieving',
CRAFTING = 'crafting',
FLETCHING = 'fletching',
SLAYER = 'slayer',
HUNTER = 'hunter',
MINING = 'mining',
SMITHING = 'smithing',
FISHING = 'fishing',
COOKING = 'cooking',
FIREMAKING = 'firemaking',
WOODCUTTING = 'woodcutting',
FARMING = 'farming'
}
...
我希望能够运行这样的代码
const OSRS_SKILLS_VALUES: string[] = Object.keys(Runescape.SkillsEnum).map(
(key: string): string => Runescape.SkillsEnum[key]
)
并且不会抛出找不到模块的错误
【问题讨论】:
-
我遇到了同样的问题,但奇怪的是只有
ts-node... VS Code 没有报告任何问题,tsc可以完美地编译和运行我的代码!
标签: node.js typescript import enums node-modules