masala-parser icon indicating copy to clipboard operation
masala-parser copied to clipboard

Typescript external support

Open nicolas-zozol opened this issue 6 years ago • 6 comments

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/

nicolas-zozol avatar Oct 13 '17 04:10 nicolas-zozol

ParsecJ is a good source of inspiration for type definitions: https://github.com/jon-hanson/parsecj

nicolas-zozol avatar Oct 13 '17 12:10 nicolas-zozol

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.

nicolas-zozol avatar Oct 15 '17 16:10 nicolas-zozol

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"
}

nicolas-zozol avatar Oct 15 '17 19:10 nicolas-zozol

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

nicolas-zozol avatar Oct 17 '17 09:10 nicolas-zozol

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.

bd82 avatar Oct 19 '17 21:10 bd82

Yes, it will be a 100% pure JS lib, with a .d.ts available with @types

nicolas-zozol avatar Oct 20 '17 04:10 nicolas-zozol