How to use enum from typescript definition file?

First your import is incorrect as pdfmake/build/pdfmake has no default export. 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.


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.

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

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

const enum 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.

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


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

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.


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.

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. Inside a "declare module", the compiler does not try to implement the object. Compare example 1 vsexample 2 on the TypeScript playground to understand the effect.

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.)

Let me know if that isn't clear.