masala-parser
masala-parser copied to clipboard
Typescript external support
Writing a definition file embedded in the package, implementing the @types system: https://blog.angular-university.io/typescript-2-type-system-how-do-type-definitions-work-in-npm-when-to-use-types-and-why-what-are-compiler-opt-in-types/
ParsecJ is a good source of inspiration for type definitions: https://github.com/jon-hanson/parsecj
package.json must reference the typings:
"typings": "./masala.d.ts",
The problem is that typings is obsolete, so it could move quickly. Another option is that there is a convention so that, because package.json
points on src/lib/index.js
an entry point, then the @types system will search for src/lib/index.d.ts
as definition file.
Found it: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html
In package.json:
{
"name": "@masala/parser",
"main": "./src/build/index.js",
"types": "./masala.d.ts"
}
Good news, we can make solid overloading in type definitions with Typescript:
function reverse(string: string): string;
function reverse<T>(array: T[]): T[];
https://blog.mariusschulz.com/2016/08/18/function-overloads-in-typescript
Hello.
If you plan on developing the d.ts definitions APIs as part of this repository. But keep this repository as a vanilla JS project.
There is a way to do so, while also ensuring that the APIs match the implemntation using new TypeScript compiler options.
See: https://github.com/bd82/typescript_for_public_apis For details on this suggested pattern.
Yes, it will be a 100% pure JS lib, with a .d.ts available with @types