typed-function (current stable version 4.2.2) is a JavaScript utility library designed to centralize and manage runtime type checking and automatic type conversion for function arguments. It allows developers to define multiple signatures for a single function, supporting union types, the `any` type, and variable arguments. This approach helps in separating type validation logic from core business logic, leading to cleaner code and more informative error messages when functions are called with incorrect input. The library aims to improve robustness and debuggability in JavaScript applications by providing consistent and helpful error messages, preventing silent failures due to invalid inputs. It is actively maintained and supports various environments, including Node.js (>=18) and modern browsers. Its key differentiators include flexible signature definition, detailed error reporting, and optional type conversion.
npm install typed-functionVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating basic typed functions with single and multiple signatures, including union types, and handling type mismatch errors.
Apply `typed-function` selectively to public API functions or where robust input validation is critical, avoiding overuse in performance-sensitive internal logic. Profile your application to identify bottlenecks.
Balance strict type checking with the robustness principle. Only apply stringent type validation where necessary for correctness, API stability, or preventing specific classes of bugs, allowing more flexibility in internal utility functions.
Always consult the official release notes and migration guides when upgrading to a new major version to understand specific breaking changes and necessary code adjustments. Test thoroughly after upgrading.
Ensure the arguments provided to the typed function strictly match one of its defined signatures. If type conversion is desired, ensure the necessary types and conversion rules are registered via `typed.addType`.
Review the function's defined signatures and adjust the call to match an existing signature. Alternatively, add a new signature to the typed function that accommodates the intended argument types and count.
No dependency data recorded yet.