feat: add typescript type declarations for analyzeCommits - #961
Open
Mearman wants to merge 1 commit into
Open
Conversation
This package has never shipped a .d.ts file or a "types" field in package.json, and no @types/semantic-release__commit-analyzer exists on npm, so a TypeScript consumer gets an implicit `any` for the whole module. Add index.d.ts declaring `analyzeCommits`, its `PluginConfig` and `ReleaseRule` parameter shapes, and its `Promise<ReleaseType | null>` return type. The shapes are drawn directly from the existing JSDoc in index.js and lib/load-release-rules.js, and from the options table in the README (which additionally documents `presetConfig`). `ReleaseType` and `AnalyzeCommitsContext` are imported from semantic-release's own published types rather than redeclared, since a consumer already has semantic-release installed as this plugin's peer dependency. Update "exports" to add a "types" condition, since moduleResolution bundler/node16/nodenext only resolve declaration files through a "types" condition inside "exports" and otherwise ignore a top-level "types" field entirely. Keep the top-level "types" field too, for classic Node resolution, and list index.d.ts in "files" so it is actually included in the published package.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This package doesn't ship any TypeScript types right now — no
.d.tsfile, notypesfield inpackage.json— and there's no@types/semantic-release__commit-analyzeron npm either, so anyone callinganalyzeCommitsfrom TypeScript gets an implicitanyfor the whole module.This adds an
index.d.tstypinganalyzeCommits(pluginConfig, context)and its return value. The shapes come straight from the JSDoc already onanalyzeCommitsinindex.jsand on the loader functions inlib/, plus the options table in the README (presetConfigin particular isn't in the JSDoc but is documented there).ReleaseTypeandAnalyzeCommitsContextare imported fromsemantic-release's own published types rather than redeclared, since anyone using this plugin already hassemantic-releaseinstalled as a peer dependency.I also had to change
exportsfrom the bare"./index.js"string to an object with atypescondition — undermoduleResolution: bundler/node16/nodenext, TypeScript only looks for a declaration file through atypescondition insideexportsonceexportsexists at all, it doesn't fall back to the top-leveltypesfield. I kept the top-leveltypesfield too for anyone still on classic resolution, and addedindex.d.tstofilesso it's actually in the published tarball.I checked this actually resolves correctly (not just "looks right") by packing the module with
npm pack, installing the tarball into a scratch project, and compiling a small consumer file against it underbundler,node16,nodenext, and classicnode/node10resolution — all four pick up the types correctly, and@ts-expect-erroron a couple of intentionally-wrong calls (missingpluginConfig, aReleaseRulemissingrelease) confirms it's real type-checking and not just resolving toany.npm testandpublint --strictboth pass unchanged.