Skip to content

feat(plugin): type custom Plugin props and resolve experimental types under node resolution - #1448

Merged
kabaros merged 8 commits into
masterfrom
feat/plugin-typescript-dx
Dec 2, 2026
Merged

kabaros merged 8 commits into
masterfrom
feat/plugin-typescript-dx

Conversation

@kabaros

@kabaros kabaros commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Two problems hit TypeScript apps that host a plugin with <Plugin>:

  1. Custom props don't type-check. Plugin forwards any extra props to the plugin at runtime, but its type only declares the built-in ones. So <Plugin pluginShortName="x" orgUnitId={id} /> fails to compile, and hosts end up casting.
  2. @dhis2/app-runtime/experimental doesn't resolve under "moduleResolution": "node", which is the default in create-app's TypeScript templates. That resolution mode ignores package exports, so tsc can't find the subpath's types.

Changes

  • Plugin is now generic over the plugin's props. <Plugin<FacilityMapProps> pluginShortName="facility-map" orgUnitId={id} onSelect={onSelect} /> checks the custom props. Without a type argument, any custom props are accepted, so existing code keeps compiling.
  • onError and showAlertsInPlugin are now part of the props type. Both were already forwarded to the plugin and handled by its wrapper. propsToPassNonMemoized?: any is removed from the public type.
  • PluginProps and PluginComponentProps are exported from @dhis2/app-runtime/experimental.
  • runtime/package.json gets a typesVersions entry for experimental, so the subpath's types resolve under node resolution too.
  • docs/components/Plugin.md explains which props reach the plugin (the plugin-side loader strips the internal ones), documents <Plugin<MyProps>>, and keeps a note on workarounds for older versions.

Testing

  • The plugin package's tests and eslint pass.
  • I type-checked a test project against the build under both "moduleResolution": "node" and "bundler". A wrong prop type and a missing prop are caught as errors (checked with @ts-expect-error), and untyped usage still compiles.

Related PRs

Part of a set that makes DHIS2 plugins (Capture form field / enrollment / bulk data entry, Dashboard) easier to build, and brings the docs in line with what the host apps actually pass:

🤖 Generated with Claude Code

kabaros and others added 4 commits December 1, 2026 09:58
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…peScript hosts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ntal types with node resolution

- make Plugin generic over the plugin's props: <Plugin<MyProps> ... />
  type-checks custom props; without a type argument any custom props
  are accepted (previously they were rejected by the compiler even
  though they are forwarded at runtime)
- declare onError and showAlertsInPlugin, which are forwarded to and
  handled by the plugin wrapper
- export PluginProps and PluginComponentProps from
  @dhis2/app-runtime/experimental
- add typesVersions so '@dhis2/app-runtime/experimental' resolves with
  "moduleResolution": "node" (the create-app template default)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@KaiVandivier KaiVandivier left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🙌

I had Claude AI do a review, and I pushed a couple fixes for these things:

  1. The "Older versions" note in the docs is never closed: docs/components/Plugin.md:99. The closing ::: sits at the start of a text line (::: For the props passed by the core apps, ...). Docusaurus only closes the box when ::: is alone on its line. As written, the rest of the page, including "Extended example", renders inside the box, or the build warns. Put ::: on its own line, then a blank line. The "core apps" paragraph probably belongs outside the note anyway.

  2. A props type written as an interface is rejected: services/plugin/src/Plugin.tsx:102, and the same constraint at line 78. TPluginProps extends Record<string, unknown> only accepts type aliases, because interfaces have no index signature. For example, interface P { foo: string } with <Plugin<P> ... /> fails with TS2344 ("Index signature for type 'string' is missing"). Many codebases write props as interfaces by default. Loosening the constraint to object (or Record<string, any>) on both lines fixes it.

  3. The "Older versions" note doesn't name a version: Plugin.md:98. "Before this change" means nothing to someone reading the published docs. Naming the release (for example "Before vX.Y.Z") tells readers whether they still need the React.FC cast.

Then there were these minor things -- they could maybe be improved, but not sure if they're important; I'll leave it up to you:

  1. Some custom prop names compile but are silently replaced: Plugin.tsx:180. The affected names are alertsAdd, setPluginHeight, setPluginWidth and setInErrorState. They aren't blocked by the type the way PluginProps keys are, but the host's own values are spread over them before the props reach the iframe. Either exclude them in the type (Partial<Record<..., never>>) or document them as reserved.

  2. The experimental fix only covers types: runtime/package.json:20. The new typesVersions entry makes @dhis2/app-runtime/experimental type-check with moduleResolution: node. But there is still no experimental/ folder or experimental.js at the package root, so tools that ignore the exports field still can't load the JavaScript. Examples are Jest before v28 and webpack 4: the import compiles, then fails at run time with "Cannot find module". Add a root experimental/package.json that points to build/cjs and build/es, or say in the docs that only the types are fixed.

@commitlint/config-conventional 21.3.0 adds the header-breaking-change
rule, which the commitlint bundled in
JulienKode/pull-request-name-linter-action@v20.1.0 does not implement,
so lint-pr-title fails on every pull request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@kabaros
kabaros marked this pull request as ready for review December 2, 2026 08:43
@kabaros
kabaros merged commit 3f10d24 into master Dec 2, 2026
16 checks passed
@kabaros
kabaros deleted the feat/plugin-typescript-dx branch December 2, 2026 08:44
dhis2-bot added a commit that referenced this pull request Dec 2, 2026
# [3.18.0](v3.17.6...v3.18.0) (2026-12-02)

### Features

* **plugin:** type custom Plugin props and resolve experimental types under node resolution ([#1448](#1448)) ([3f10d24](3f10d24))
@dhis2-bot

Copy link
Copy Markdown
Contributor

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants