如何使用打字稿定义文件中的枚举? [英] How to use enum from typescript definition file?

查看:23
本文介绍了如何使用打字稿定义文件中的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 typescript 在 nodejs 中编程.

I'm programming in nodejs using typescript.

我正在使用 pdfmake我为这个库安装了打字稿定义文件(@types/pdfmake)

I'm using pdfmake and I installed typescript definition files for this library (@types/pdfmake)

https://github.com/DefinitelyTyped/绝对Typed/blob/master/types/pdfmake/index.d.ts

我正在从这个文件中导入一个枚举

I'm importing one of the enums from this file

import { PageSize } from "pdfmake/build/pdfmake";

并像这样使用它PageSize.A4

这可以编译,但是当我尝试访问此枚举的值 A4 时,发生运行时错误

This compiles, but when I try to access value A4 of this enum, runtime error occurs

类型错误:无法读取未定义的属性A4"

TypeError: Cannot read property 'A4' of undefined

任何帮助将不胜感激

package.json:

package.json:

{
  "name": "nodejs-pdf-make",
  "version": "1.0.0",
  "description": "",
  "main": "dist/app.js",
  "scripts": {
    "start": "tsc && node --inspect dist/app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.1",
    "tslint": "^5.19.0",
    "typescript": "^3.6.2"
  },
  "dependencies": {
    "@types/pdfmake": "^0.1.8",
    "express": "^4.17.1",
    "pdfmake": "^0.1.58"
  }
}

tsconfig.json:

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  },
  "lib": ["es2015"]
}```

推荐答案

问题是类型定义只针对类型.它们不会改变模块的运行时行为.因此,虽然 TypeScript 对你的代码很满意,因为类型定义说这个模块导出一个枚举 PageSize,但在运行代码时会崩溃,因为实际上模块不导出 PageSize>.

The problem is that type definitions are just for types. They don't change the runtime behavior of a module. So while TypeScript is happy with your code because the type definitions say that this module exports an enum PageSize, this falls apart when running the code because in reality the module does not export PageSize.

我认为这是 @types/pdfmake 中的一个错误.

I would consider this a bug in @types/pdfmake.

这个问题可以在 @types/pdfmake 中通过使 enum const 解决:

This problem could be resolved in @types/pdfmake by making the enum const:

const enum PageSize {
   // ...
}

TypeScript 自动内联 const 枚举.内联意味着编译器将 PageSize.A4 替换为 'A4',这样 JavaScript 代码中就没有对 PageSize 的引用.

TypeScript automatically inlines const enums. Inlining means the compiler replaces PageSize.A4 with 'A4' so that there are no references to PageSize left in the JavaScript code.

当然,这需要对 @types/pdfmake 进行更改.我鼓励您在 DefinitelyTyped 存储库 上打开拉取请求或问题.

Of course this would require a change in @types/pdfmake. I encourage you to open a pull request or issue on the DefinitelyTyped repo.

@types/pdfmake 没有任何变化的情况下,您唯一的选择是使用硬编码字符串:

Without any changes in @types/pdfmake the only option you have is use hardcoded strings:

pageSize: 'A4' as PageSize

不幸的是,您甚至可能不得不将其强制转换为 PageSize 枚举,因为这是类型定义所期望的.

Unfortunately you'll probably even have to cast it to the PageSize enum, because that's what the type definitions expect.

这篇关于如何使用打字稿定义文件中的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆