Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically generate type declarations 🪄 #1349

Open
alexweininger opened this issue Jan 13, 2023 · 0 comments
Open

Automatically generate type declarations 🪄 #1349

alexweininger opened this issue Jan 13, 2023 · 0 comments

Comments

@alexweininger
Copy link
Member

We are currently manually maintaining an index.d.ts declaration file in each package.

Why this isn't good:

  • We have to manually update declaration file -> easy to make mistakes
    • No checking to make sure something wasn't added
    • No checking to make sure types are valid and match the source
  • Have to import types from '../../../index' -> ugly 😢

The TypeScript compiler can generate declaration files from source .ts files. It takes every exported source file [source].ts and generates a corresponding [source].d.ts declaration file. However, we often want to hide certain properties, methods, etc. from the public api of our libraries. Which isn't possible by only using tsc.

Currently, we declare an internal interface, and then declare a public interface in the declaration as a way of hiding internal things. This is cumbersome to write, and adds confusion in the source code.

And that's when the @microsoft/api-extractor package comes to save the day. 🦸

Their website has a great summary of the package. It can rollup generated declaration files into a single declaration file to include in the npm package. On top of that, it leverages TSDoc for some really nice features. For example, excluding things marked with @internal, or separating @beta declarations for developing new APIs.

@alexweininger alexweininger changed the title Automatically generate type declarations Automatically generate type declarations 🪄 Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant