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! 🚀 |
|
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.
typescript@3.8.lint-typesscript, replaced bybuild:package-types.tsconfig.base.jsonthat packages extend. This is not a project, it's only for extension.tsconfig.jsonthat references all typed packages.build-typesand expose them in the published package.TypeScript declaration files will be managed via
tsc --build, the TypeScript compiler build tool for composite projects.tscwill 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.jsonin their package directory.package.jsonto 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.tsand*.tsbuildinfofiles should be removed.npm run build:packagesshould generate the typesGenerated type definitions can be inspected under
packages/*/build-types. For example:@wordpress/is-shallow-equalGenerated:
index.d.tsarrays.d.tsobjects.d.tsManual type declaration:
@wordpress/i18nGenerated
index.d.tsManual
Try using some of the typed packages in an existing project, preferably via TypeScript. Are the type definitions correctly exposed?