简体   繁体   中英

TypeScript - how to write a declaration file for a third party package with a single global function?

I'm trying to write a declaration file for a third party package ( latex ). The package exports a single global function latex() . I tried to use the following declaration file ( src/@types/latex/index.d.ts ):

declare module 'latex' {
    import { Stream } from "stream";
    export function latex(latex: string | string[] | Buffer | Stream): Stream
}

Now I'm trying to import latex like this import * as latex from 'latex' but the compiler force me to invoke latex.latex() instead of just latex() .

How can I fix this ?

Thank you.

If the function is the default export, you can add the default keyword in your definition file:

export default function latex(...

And then you can import it and use it like this:

import latex from 'latex';

latex(args);
export default function latex()

This is ES6 modules syntax. But the original code of the latex module is using commonjs module syntax. The declaration file should therefore be

export = function latex()

Importing should be also done using commonjs way in TypeScript

import latex = require('latex')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM