Skip to content

TypeScript conversion with full API parity (v0.11.0) - #31

Merged
mattthehat merged 20 commits into
masterfrom
exp-ts
Jul 23, 2026
Merged

TypeScript conversion with full API parity (v0.11.0)#31
mattthehat merged 20 commits into
masterfrom
exp-ts

Conversation

@mattthehat

@mattthehat mattthehat commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

Converts Dabby from JavaScript to TypeScript with full API parity, and prepares the package for release as v0.11.0.

TypeScript conversion

  • All source modules rewritten in TypeScript under strict: true; tsc emits per-module .d.ts files (with declaration maps) alongside the vite bundle
  • Modular type system via module augmentation: importing a method module adds its types to $() automatically — documented in TYPESCRIPT.md
  • createDabby<'html' | 'on'>() alternative for explicitly enumerating available methods, with correct this-chaining via the Self generic
  • Trusted Types support: all innerHTML sinks route through a pass-through trustedTypes policy; TrustedHTML accepted by $(), .html(), .append(), etc.

API parity with the JS version

Verified by building both versions and diffing the runtime API of the bundles:

  • No methods missing. All statics ($.ajax, $.get, $.post, $.getScript, $.param, $.each, $.extend, $.map, $.parseHTML, $.isPlainObject) and all $.fn methods are present
  • New additions: $.fn.even(), $.fn.odd(), $.isArray, $.isFunction, $.isWindow
  • Only structural difference: length is now an instance property rather than a prototype getter; $().length behaves identically

Packaging

  • Version bumped to 0.11.0
  • "type": "module" added (fixes Node ESM module-type warning)
  • exports map with per-method subpaths (dabbyjs/manipulation/html/html) plus . (modular entry) and ./full (complete bundle)
  • Tarball slimmed: test files excluded from files (485 → 333 files, 806 kB → 569 kB unpacked)

Testing & CI

  • 364 unit tests across 70 files (vitest + happy-dom), all passing
  • tsd type tests in test-d/, plus full-project tsc --noEmit under strict mode
  • GitHub Actions workflows covering build, type-check, tsd, and tests on Node 20/22

This commit introduces full TypeScript support for Dabby.js, including:

- TypeScript source files for all modules with complete type definitions
- Modular build system with automatic type inference via module augmentation
- Type-safe factory functions (createDabby<Methods>() and auto-inferred $)
- Comprehensive type definitions for all methods and overloads
- Full IntelliSense support in modern IDEs

Build system updates:
- Added TypeScript compilation to Grunt build pipeline
- Integrated grunt-ts for automated TypeScript compilation
- Updated Gruntfile to compile TypeScript before rollup bundling
- Modified watch tasks to monitor .ts files for changes
- Fixed 56 TypeScript files with incorrect dist/ imports

Type testing infrastructure:
- Installed tsd for comprehensive type testing (replaces deprecated dtslint)
- Created extensive type tests covering all major functionality
- Tests validate factory types, method signatures, callbacks, and chaining
- All type tests passing with zero errors
- Added npm scripts: test:types, tsc, and build

Package configuration:
- Updated package.json to include TypeScript files and type definitions
- Added type definition files to npm package distribution
- Configured tsd.json for proper type test resolution
- Updated tsconfig.json for ES2022 module compilation

Documentation:
- Added comprehensive TypeScript section to README
- Documented modular TypeScript builds with examples
- Included build commands and type testing instructions
- Updated getting started section with TypeScript usage

Type coverage includes:
- Core factory and selector types (Dabby, DabbyFactory, Selector)
- HTML/text manipulation methods with getter/setter overloads
- Event handling (on, off, one, trigger) with proper callback types
- Attributes and CSS manipulation with union types
- DOM traversal methods with type preservation
- Modular build types (DabbyWithMethods, DabbyAuto)
- Static utility methods (map, extend, isPlainObject, parseHTML)

All 18 initial type errors resolved through proper type assertions and
accurate type definitions matching runtime behaviour.
…files, and new utility functions; include comprehensive test reports and ensure backward compatibility with type augmentation.
- Add overloaded signatures for `on`, `one`, and `off` event methods to
  replace loose optional parameters with explicit call signatures
- Change traversal and manipulation return types from `Dabby` to `this`
  to preserve augmented method types through chaining
- Change `DabbyAuto` from `Omit & Intersection` to `Dabby & ModularDabbyMethods`
  so it remains assignable to `Dabby` as required by augmented methods
- Add missing `$.get(url, data)` and `$.post(url, data)` overloads
- Fix `$.map` callback key parameter to `number | string` matching
  runtime behaviour where arrays receive numeric keys
- Fix `val()` return type removing `number` (DOM values are always strings)
- Fix `attr()` getter return type to `string | null` matching DOM API
- Make `removeClass()` parameter optional to allow removing all classes
- Add missing `appendTo`, `insertAfter`, `insertBefore`, `prependTo`
  method stubs
- Fix non-null assertions in `clone.ts` and `named.ts` to satisfy
  stricter overloaded signatures
- Removed method signatures from Dabby class to streamline the codebase.
- Updated offset, offsetParent, position, scroll, width, and height methods to return appropriate types in ModularDabbyMethods.
- Enhanced event handling methods (on, one, off, trigger, triggerHandler) with specific return types.
- Improved manipulation methods (clone, remove, replace, wrap, unwrap) to ensure type safety and consistency.
- Refined traversal methods (closest, has, index, last, next, prev, parent, parents, siblings, slice) to return correct types.
- Updated utility methods (each, extend, map) to enhance functionality and type definitions.
- Ensured all changes maintain compatibility with existing Dabby functionality while improving type safety and clarity.
- Removed unused type imports from various modules to clean up code.
- Enhanced type definitions for Dabby methods to support method chaining with Self parameter.
- Added modular augmentations for methods like load, add, find, and others to improve modular build compatibility.
- Introduced new type witnesses to ensure TypeScript includes necessary file augmentations.
- Created a new test file to demonstrate usage and explore available methods.
- Added Vite configuration for building the library with sourcemaps and minification.
- Updated build and build:types scripts in package.json to use tsconfig.build.json.
- Changed module declarations from 'dabbyjs' to '../../modular.js' across multiple files for better modular compatibility.
- Removed unused html augmentation declaration files.
- Added tsconfig.build.json for better build configuration.
- Updated test.ts to validate augmented methods.
…' instead of 'modular.js'

- Changed all import statements from '../../modular.js' to '../../dabby.js' in traversal and utility files.
- Updated module augmentation declarations to reflect the new import path.
- Created a new 'dabby.js' file to serve as a modular export helper, allowing for better type inference and method availability.
- Adjusted test files to import from 'dabby.js' instead of 'modular.js'.
- Modified TypeScript configuration to align with the new module structure.
- Updated Dabby constructor to accept TrustedHTML as a valid selector type.
- Enhanced HTML manipulation methods to utilize TrustedHTML for safer innerHTML assignments.
- Introduced even() and odd() methods for selecting even and odd indexed elements.
- Implemented Trusted Types API for secure HTML handling.
- Updated parseHTML function to support TrustedHTML and improved script handling.
- Added tests for new even() and odd() methods, as well as for event handling and property retrieval.
- Removed deprecated test.ts file and adjusted TypeScript configuration for better module handling.
- Implement tests for traversal methods: even, odd, first, last, find, filter, has, index, next, prev, and their respective variations.
- Create utility function tests for each, extend, isFunction, isPlainObject, isWindow, map, parseHTML, and each.
- Ensure all tests cover various scenarios including edge cases for empty collections and non-matching selectors.
- Set up Vitest configuration for testing environment and module resolution.
Added tsd config in package.json pointing to test-d/ directory
Added /// <reference> to src/dabby.ts for trusted-types.d.ts so tsd can find the TrustedHTML type
Removed invalid expectError for $el.html(123) — TrustedHTML interface makes numbers assignable
Added build step to pr-checks.yml type-check job (needed for .d.ts files)
- Added comprehensive JSDoc comments for the `add`, `children`, `closest`, `eq`, and `even` methods, including parameters, return types, and usage examples.
- Updated README files for the `add`, `children`, `closest`, `eq`, and `even` methods to reflect new signatures and provide clearer usage instructions.
- Introduced a new `filter` method to determine element membership and a `not` method to exclude elements based on a selector.
- Modified the `parseHTML` utility to accept `TrustedHTML` and return `Element[]`.
- Improved TypeScript configuration by excluding test and benchmark files from compilation.
- Removed obsolete `tsd.json` file and updated `vite.config.ts` for better TypeScript support.
- Created demo files to showcase type inference and autocomplete features in IDEs.
- Updated $.extend() to improve clarity on shallow and deep merging, including examples and parameter descriptions.
- Revised $.isFunction() to provide a type guard and updated its documentation to reflect its internal usage.
- Enhanced $.isPlainObject() with detailed descriptions and examples for better understanding of plain object checks.
- Improved $.isWindow() to clarify its purpose as a type guard for the global window object.
- Added comprehensive documentation for $.map() to explain its functionality and provide usage examples.
- Updated $.parseHTML() to clarify its behavior with script execution and added examples for practical use cases.
- Add isArray, isFunction, isWindow utility modules with tests and docs
- Bump version to 0.11.0 (master published 0.10.1)
- Add "type": "module" to package.json to fix Node ESM module-type warning
- Slim npm tarball: exclude src test files, drop stale src/**/*.js glob
  (485 -> 333 files, 806kB -> 569kB unpacked)
- Fix docs: use exports-map subpaths (dabbyjs/...) instead of dabbyjs/src/...
  in TYPESCRIPT.md examples, correct exports example to match package.json,
  update README import example (dabby.min.js no longer exists)
@mattthehat mattthehat changed the title Exp ts TypeScript conversion with full API parity (v0.11.0) Jul 20, 2026
Resolve conflicts between the TypeScript migration and master's
grunt-to-vite move:

- package.json: keep the exp-ts version, which already supersedes
  master's vite migration. Carried over master's terser bump to ^5.46.0.
- tsconfig.json: keep it. Master deleted it when dropping TypeScript as
  a dependency, but this branch is the TypeScript migration.
- package-lock.json: regenerated from the resolved package.json rather
  than hand-merged.
- vite.config.js: removed. Master added it, but vite.config.ts on this
  branch replaces it, and Vite resolves the .js config first, which
  would have silently built src/build.js instead of src/build.ts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

⚠️ Bundle Size Report 📉

Metric Size
Base (master) 49.57 KB
PR (exp-ts) 2.30 KB
Difference -48405 B (-95.36%)

⚠️ Warning: Bundle size changed by more than 5%

@mattthehat
mattthehat merged commit 6effc74 into master Jul 23, 2026
32 checks passed
@mattthehat
mattthehat deleted the exp-ts branch July 23, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant