Skip to content

Testing: Add TypeScript types validation for modules #18838

Closed

Description

Previously: #17014

While Gutenberg packages are not authored in TypeScript, we can still benefit from its JavaScript type checking using the JSDoc we already write. This will bring us some benefit of type safety, even as we continue to write modules with JavaScript, not TypeScript.

Progress:

All of our modules are to be opted-in to type checking. The progress thus far:

Guidelines:

See the packages README for details on TypeScript usage. In summary, a package can opt-in by following these steps:

  • Add a tsconfig.json to the package root. The @wordpress/i18n package has a good example of a basic config.
  • Add "types": "build-types" to the package's package.json so that consumers will pick up the published types from the package.
  • Add the package to the root tsconfig.json references.

For single files, you can also include a // @ts-check line at the top of the file. You should use this line when temporarily testing the impact of type checking, or if you are working to convert a very large package one file at a time.

The build:package-types npm script will be run as part of precommit, which handles type checking. It can be very helpful to watch the types build and work through issues surfaced by the compiler as you add types. To watch the types build, run:

npm run build:package-types -- --watch

You may also need to improve existing JSDoc, as it was written based on assumed usage. Type checking will enforce its validity, and in many cases our JSDoc is invalid.

For syntax and usage, reference:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    [Status] In ProgressTracking issues with work in progress[Type] Automated TestingTesting infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.[Type] Code QualityIssues or PRs that relate to code quality[Type] TaskIssues or PRs that have been broken down into an individual action to take[Type] Tracking IssueTactical breakdown of efforts across the codebase and/or tied to Overview issues.npm PackagesRelated to npm packages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions