ts-interface-checker is a runtime validation library for TypeScript interfaces, currently stable at version 1.0.2. It works in conjunction with `ts-interface-builder` (a build-time tool) to generate JavaScript modules that encapsulate the structural checks derived from TypeScript interface definitions. This approach allows developers to validate arbitrary JavaScript objects (e.g., parsed JSON from network requests or configuration files) against their TypeScript types at runtime, providing detailed error messages upon failure. The library itself is lightweight and focuses solely on the runtime checking mechanism, making it suitable for environments where type safety beyond compile-time is desired, such as API boundary validation. It supports checking properties, optional fields, and even method call arguments and return values as defined in interfaces. It maintains a stable release cadence for its core functionality.
npm install ts-interface-checkerVerified import paths — ran on the pinned version, not inferred.
Demonstrates the full workflow of defining a TypeScript interface, conceptually generating runtime checkers using `ts-interface-builder`, and then utilizing `ts-interface-checker` to validate JavaScript objects against the interface's structure at runtime, including expected error scenarios.
Ensure `npm install --save-dev ts-interface-builder` and `npm install --save ts-interface-checker` are executed, and run `npx ts-interface-builder <your-ts-files>` as part of your build process before runtime checks are performed.
Avoid `strictCheck()` for interfaces that are part of public APIs or stable data contracts to allow for graceful schema evolution. Use standard `check()` unless strict enforcement of known properties is absolutely required.
Refactor interfaces to avoid complex generics or manually implement runtime checks for parts of the schema that rely on unsupported generic constructs. Refer to `ts-interface-builder` limitations for details.
Ensure the object being validated includes all non-optional properties defined in the TypeScript interface. For example, `Square.check({ size: 1 });` instead of `Square.check({ color: 'green' });` for an interface requiring `size`.Verify that all properties in the validated object conform to their specified TypeScript types. For example, `Square.check({ size: 4, color: 'blue' });` instead of `Square.check({ size: 4, color: 5 });`.Ensure arguments passed to `methodArgs().check()` match the types defined for the method's parameters in the interface. For example, `Greeter.methodArgs("greet").check(["Bob"]);` instead of `Greeter.methodArgs("greet").check([17]);`.Provide all required arguments when calling `methodArgs().check()`. For example, `Greeter.methodArgs("greet").check(["Alice"]);` instead of `Greeter.methodArgs("greet").check([]);`.Ensure the return value passed to `methodResult().check()` matches the method's return type. For example, `Greeter.methodResult("greet").check("hello");` instead of `Greeter.methodResult("greet").check(null);`.No dependency data recorded yet.