christianp / asciimath2tex

JavaScript library to convert AsciiMath to TeX
Apache License 2.0
70 stars 16 forks source link

Please add type declarations to npm package #17

Closed Stefaans closed 2 years ago

Stefaans commented 2 years ago

To use asciimath2tex in a TypeScript project, we need type definitions. The following works for me and may be a solution for you too:

In packages.json, specify the type definitions file:

"typings": "type.d.ts",

Add file type.d.ts:

export as namespace asciimath2tex;

class AsciiMathParser {
  constructor();
  parse(str): string;
}

export default AsciiMathParser;

With the type definitions in place, I can do:

import AsciiMathParser from 'asciimath2tex';
...
...
const parser = new AsciiMathParser();
const tex = parser.parse("int_(i=1)^10 x^2/2 dx");
christianp commented 2 years ago

Can you make a pull request that does this, please? I don't use TypeScript so don't know what needs to be done or how to test this.

Stefaans commented 2 years ago

Thank you Christian. I have submitted a pull request.

The solution was simpler than I expected. It was a simple case of specifying the types definition file in packages.json, and microbundle took care of the rest. The asciimath2tex.d.ts file that microbundle generates is bit more verbose than necessary, but good enough because it contains the essential definitions for the AsciiMathParser class and its parser method.

I was not familiar with microbundle until today. Learning something new every day :)

Stephen

christianp commented 2 years ago

fixed by #19