fix(core): improve native TypeScript type definitions#35251
fix(core): improve native TypeScript type definitions#35251AgentEnder wants to merge 5 commits intomasterfrom
Conversation
The auto-generated `index.d.ts` from NAPI-RS referenced several types that were never declared: `NxDbConnection`, `ParserArc`, `WriterArc`, `HashInstruction`, `JsInputs`, `ProjectFiles`, `ProjectRootMappings`, and `NapiDashMap`. This caused TypeScript compilation failures when consuming the `nx` package. For transparent types with concrete TypeScript equivalents (`JsInputs`, `ProjectFiles`, `ProjectRootMappings`, `NapiDashMap`), add `#[napi(ts_type)]` annotations to the Rust source so NAPI-RS generates correct inline types. For opaque types only used inside `ExternalObject<T>` (`NxDbConnection`, `ParserArc`, `WriterArc`, `HashInstruction`), add a global ambient `.d.ts` file with branded interface declarations to preserve type safety. Fixes #32205 https://claude.ai/code/session_01HRaZd7KV54jKh3BFr2ZdKu
…al declarations Replace the ambient global native-bindings-types.d.ts with a dts header file that gets prepended to the generated index.d.ts by napi-rs. This keeps the branded types (NxDbConnection, ParserArc, WriterArc, HashInstruction) scoped to the module rather than leaking them into the global namespace for consumers of nx. https://claude.ai/code/session_01HRaZd7KV54jKh3BFr2ZdKu
✅ Deploy Preview for nx-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for nx-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
View your CI Pipeline Execution ↗ for commit f1cd6a8
☁️ Nx Cloud last updated this comment at |
Adds a regression test that runs TypeScript's type checker against the generated index.d.ts with skipLibCheck disabled. This catches undefined type references that would otherwise slip through since the base tsconfig has skipLibCheck enabled. https://claude.ai/code/session_01HRaZd7KV54jKh3BFr2ZdKu
05a7ac9 to
efc08e0
Compare
There was a problem hiding this comment.
✅ The fix from Nx Cloud was applied
We add a .gitignore negation exception for packages/nx/src/native/dts-header.d.ts to fix the no-ignored-tracked-files conformance violation. The existing packages/nx/**/*.d.ts pattern was catching this newly introduced hand-authored header file, causing Nx hashing issues. This follows the same pattern already in place for index.d.ts and other intentionally tracked .d.ts files.
Tip
✅ We verified this fix by re-running nx-cloud record -- pnpm nx conformance:check.
Suggested Fix changes
diff --git a/.gitignore b/.gitignore
index 0312763e..3ac18d26 100644
--- a/.gitignore
+++ b/.gitignore
@@ -152,5 +152,6 @@ packages/nx/**/*.d.ts
!packages/nx/src/utils/perf-hooks.d.ts
!packages/nx/src/ai/set-up-ai-agents/schema.d.ts
!packages/nx/src/native/index.d.ts
+!packages/nx/src/native/dts-header.d.ts
e2e/**/*.d.ts
e2e/**/*.d.ts.map
Note
Auto-apply was skipped. The previous CI pipeline execution was triggered by Nx Cloud
🔔 Heads up, your workspace has pending recommendations ↗ to auto-apply fixes for similar failures.
View interactive diff ↗➡️ This fix was applied by Craigory Coppola
🎓 Learn more about Self-Healing CI on nx.dev
Co-authored-by: AgentEnder <AgentEnder@users.noreply.github.com>
Current Behavior
The native TypeScript type definitions in
index.d.tswere auto-generated with minimal type information, using generic type aliases likeProjectFilesandJsInputsthat obscured the actual underlying types. This made it harder for TypeScript consumers to understand the actual structure of data being passed to and returned from native functions.Expected Behavior
TypeScript definitions should be explicit and clear about the actual types being used, with proper branded types for opaque native handles and expanded union types for input specifications. This improves IDE autocomplete, type checking, and developer experience.
Changes
Added branded types for native handles: Created opaque branded interfaces (
NxDbConnection,ParserArc,WriterArc,HashInstruction) to prevent accidental interchangeability of differentExternalObject<T>handles.Expanded type definitions: Replaced generic type aliases with explicit types:
ProjectFiles→Record<string, Array<FileData>>JsInputs→InputsInput | string | FileSetInput | RuntimeInput | EnvironmentInput | ExternalDependenciesInput | DepsOutputsInput | WorkingDirectoryInputAdded NAPI type annotations: Updated Rust source files with
#[napi(ts_type = "...")]and#[napi(ts_arg_type = "...")]attributes to explicitly specify TypeScript types for:TaskHasher.constructor()andhashPlans()methodsWorkspaceContext.updateProjectFiles()methodTarget.inputsandProject.namedInputsfieldsNxJson.namedInputsfieldFileMapandNxWorkspaceFilesstructuresCreated DTS header file: Added
dts-header.d.tscontaining the branded type definitions, configured inpackage.jsonvia thedtsHeaderFileoption to ensure these types are included in the generated type definitions.Fixed return type: Changed
TaskHasher.hashPlans()return type fromNapiDashMap<string, HashDetails>toRecord<string, HashDetails>for clarity.Test Plan
Existing tests should pass. The changes are purely type definition improvements with no runtime behavior changes. TypeScript compilation should succeed with improved type checking accuracy.
Fixes 32205
https://claude.ai/code/session_01HRaZd7KV54jKh3BFr2ZdKu