简体   繁体   English

如何使用打字稿定义文件中的枚举?

[英]How to use enum from typescript definition file?

I'm programming in nodejs using typescript. 我正在使用typescript在nodejs中编程。

I'm using pdfmake and I installed typescript definition files for this library (@types/pdfmake) 我正在使用pdfmake,并且为此库安装了打字稿定义文件(@ types / pdfmake)

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

I'm importing one of the enums from this file 我正在从此文件导入一个枚举

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

and using it like this PageSize.A4 并像这样使用它PageSize.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 TypeError:无法读取未定义的属性“ A4”

Any help would be greatly appreciated 任何帮助将不胜感激

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"]
}```

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 . 因此,尽管TypeScript对您的代码感到满意,因为类型定义说此模块导出了枚举PageSize ,但在运行代码时却分崩离析,因为实际上该模块不会导出PageSize

I would consider this a bug in @types/pdfmake . 我认为这是@types/pdfmake的错误。

This problem could be resolved in @types/pdfmake by making the enum const : 通过使枚举const可以在@types/pdfmake解决此问题:

const enum PageSize {
   // ...
}

TypeScript automatically inlines const enums. TypeScript自动内联const枚举。 Inlining means the compiler replaces PageSize.A4 with 'A4' so that there are no references to PageSize left in the JavaScript code. 内联意味着编译器用'A4'替换PageSize.A4 ,以便在JavaScript代码中没有对PageSize引用。

Of course this would require a change in @types/pdfmake . 当然,这需要更改@types/pdfmake I encourage you to open a pull request or issue on the DefinitelyTyped repo . 我建议您在DefinitelyTyped仓库中打开拉取请求或发布。


Without any changes in @types/pdfmake the only option you have is use hardcoded strings: @types/pdfmake不做任何更改,您唯一的选择是使用硬编码字符串:

pageSize: 'A4' as PageSize

Unfortunately you'll probably even have to cast it to the PageSize enum, because that's what the type definitions expect. 不幸的是,您甚至可能必须将其PageSize转换为PageSize枚举,因为这是类型定义所期望的。

When processing an enum in a TypeScript source file (.ts) , the TypeScript compiler emits the appropriate JavaScript code to create an object which can be accessed at runtime. 在处理TypeScript 源文件(.ts)中enum时,TypeScript编译器会发出适当的JavaScript代码来创建可以在运行时访问的对象。

However when processing the TypeScript declaration file (.d.ts) in your case, the enum is has been wrapped inside a declare module "pdfmake/build/pdfmake" block. 但是,在处理您的情况下的TypeScript 声明文件(.d.ts)时, enum已包装在declare module "pdfmake/build/pdfmake"块中。 Inside a "declare module", the compiler does not try to implement the object. 在“声明模块”内部,编译器不会尝试实现该对象。 Compare example 1 vs example 2 on the TypeScript playground to understand the effect. 在TypeScript运动场上比较示例1示例2 ,以了解效果。

This behavior is by design, because the intended use-case for declaration files is enable type-checking of code written in a separate module (typically one written in JavaScript.) 此行为是设计使然,因为声明文件的预期用例是启用对在单独模块中编写的代码(通常是用JavaScript编写的模块)进行类型检查。

Let me know if that isn't clear. 让我知道是否不清楚。

First your import is incorrect as pdfmake/build/pdfmake has no default export. 首先,您的导入不正确,因为pdfmake / build / pdfmake没有默认导出。 You can use: 您可以使用:

import * as pdfMake from "pdfmake/build/pdfmake";

to get a reference to the module, but you will find that the module has no reference to the PageSize enum as it wasn't exported as Burt_Harris explained in his answer. 以获得对模块的引用,但是您会发现该模块没有对PageSize枚举的引用,因为它没有像Burt_Harris在其答案中解释的那样被导出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有定义文件的库中使用 TypeScript? - How to use TypeScript w/ a library with no definition file? 如何将 TypeScript 定义文件用于另一个 function - How to use a TypeScript definition file for another function 用于“魔术字符串”属性的TypeScript 1.8定义文件Enum - TypeScript 1.8 definition file Enum for 'magic string' properties 如何从TypeScript文件生成类型定义文件? - How to generate type definition file from TypeScript files? 来自 JSON 文件嵌套数组的 TypeScript 枚举? - TypeScript enum from JSON file nested array? 从TypeScript定义文件生成其他JS“编译器”的定义? - Generate definition for other JS “compilers” from TypeScript definition file? 如何为这个 javascript 库编写打字稿定义文件? - how to write typescript definition file for this javascript library? 如何使用 WebStorm 在 TypeScript 而不是 JavaScript 中创建 Cucumber 步骤定义文件? - How can I use WebStorm to create a Cucumber step definition file in TypeScript instead of JavaScript? 如何在TypeScript中使用不带定义文件的JS库(React-Summernote) - How to use a JS library in Typescript without a Definition file (React-Summernote) 如何从 typescript 中的单个枚举值中获取所有枚举值? - How to get all enum values from a single enum value in typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM