-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Build: Output package type declarations #18942
Conversation
8dd8476
to
ae3bb21
Compare
a232dc4
to
da33e28
Compare
da33e28
to
de3e311
Compare
Size Change: +176 B (0%) Total Size: 856 kB
ℹ️ View Unchanged
|
I did a quick sanity check and it looks good for gutenberg-mobile as of now 👍 It might impact the Monorepo work that's being done though as we needed to do some changes in |
Travis is green, there is approval from @aduth. I will leave it to you both to push the button and celebrate this moment. Awesome work 🥇 I'm looking forward to starting using it in practice – I wasn't very happy by type support in Visual Studio Code for code imported from local npm packages. It should make it way much better! :) |
Initially it will be better in some ways (up-to-date) and worse in others (our usage of types currently is not very good), but it will definitely force us to be more honest with how and where we use types. I imagine it will only get better over time! 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still looking good 👍
Thanks everyone who helped get this over the line ❤️ 🙌 |
Provide clear errors due to mismated TypeScript versions. This should help make the transition period from #18942 smoother. Add `validate-typescript-version` bin that will be used by `lint-staged` typechecker and in `build:package-types` script. This helps to prevent cryptic errors when an older TypeScript module is present in `node_modules` and requires updating. Without this change, lint-staged and `npm run build:package-types` would print this error: ```none # npm run build:pacakge-types > tsc --build error TS5053: Option 'allowJs' cannot be specified with option 'declaration'. error TS5053: Option 'declaration' cannot be specified with option 'isolatedModules'. ``` This is due to an incompatible TypeScript package. With this change, these script will print this error: ```none $ npm run build:package-types > node ./bin/packages/validate-typescript-version.js && tsc --build TypeScript dependency out of date. Detected: '3.5.3' Required: '3.8.3' Please ensure dependencies are up to date. ```
Description
Output TypeScript declaration files (
.d.ts
) from package sources and expose them with published packages.TypeScript 3.7 added support for outputting a declaration files from JavaScript sources.
The setup is largely inspired by the jest setup, which is another monorepo which has many TypeScript packages they generate types from.
[email protected]
.lint-types
script, replaced bybuild:package-types
.tsconfig.base.json
that packages extend. This is not a project, it's only for extension.tsconfig.json
that references all typed packages.build-types
and expose them in the published package.TypeScript declaration files will be managed via
tsc --build
, the TypeScript compiler build tool for composite projects.tsc
will manage determine the correct order to build packages so that dependencies are satisfied and rebuild only when necessary.Additional packages can opt-in to typing by (this is documented in a new section in
packages/README.md
):tsconfig.json
in their package directory.package.json
to point to the types:{ "types": "build-types" }
.tsconfig.json
.Testing
Verify the following scripts work as expected:
npm run build:package-types
- Build package types. Check for a generated declaration files, e.g.packages/a11y/build-types/*.d.ts
.npm run clean:package-types
-build-types/**/*.d.ts
and*.tsbuildinfo
files should be removed.npm run build:packages
should generate the typesGenerated type definitions can be inspected under
packages/*/build-types
. For example:@wordpress/is-shallow-equal
Generated:
index.d.ts
arrays.d.ts
objects.d.ts
Manual type declaration:
@wordpress/i18n
Generated
index.d.ts
Manual
Try using some of the typed packages in an existing project, preferably via TypeScript. Are the type definitions correctly exposed?