Search code examples
angulartypescripttabulatordefinitelytyped

@types/tabulator-tables breaking tabulator-tables import


Installing the following (as of [email protected]):

npm install tabulator-tables
npm install @types/tabulator-tables

And then doing the following import

import Tabulator from 'tabulator-tables';

Results in an error: Module Usage (Error node_modules @types tabulator tables index.d.ts' is not a module).

Using this import instead

import 'tabulator-tables';

outputs no error, and allows type information to be loaded, but at runtime (e.g., on an Angular 12 project) doesn't actually allow access to the Tabulator object, resulting in errors like ReferenceError: Tabulator is not defined.


Solution

  • This is due to the lack of a default export in the type definition file in @types/tabulator-tables. Unfortunately, adding a default export upstream isn't feasible because it would break existing codebases.

    Luckily there's a workaround for this, described here: create an index.d.ts file if you haven't already, and add the following line to it:

    declare module "tabulator-tables" { export = Tabulator; }
    

    (Be sure to also make sure the index.d.ts file is included by listing it in "files" or making sure it's matched by a pattern in "includes" in your tsconfig.json. You may also need to set "allowSyntheticDefaultImports": true in "compilerOptions".)