typescript-is is a TypeScript compiler transformer that generates runtime type-check functions directly from static TypeScript types. It automates the process of creating type predicates for `any` or `unknown` data, which is common when working with external data sources like API responses or user-uploaded files. The library inspects type definitions at compile time and emits JavaScript functions that meticulously validate incoming objects against those definitions. Currently at version 0.20.0, the project is officially deprecated and will not be updated for TypeScript versions 4.8 and above. Users are strongly advised to migrate to `typia` for newer TypeScript environments. Before its deprecation, the project aimed for regular, feature-driven releases. Its core differentiator lies in leveraging the TypeScript compiler API to avoid manual type predicate writing, aiming for comprehensive type-safety at runtime for serializable JavaScript objects.
npm install typescript-isVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure `typescript-is` with `ttypescript` in `tsconfig.json` and use `assertEquals` to perform runtime type validation on an `unknown` input, ensuring type safety for `User` objects. It shows both valid and invalid data scenarios.
Migrate to the recommended alternative, `typia` (https://github.com/samchon/typia), or another actively maintained runtime type-checking library for TypeScript.
Ensure `transpileOnly` is set to `false` or use `ttypescript` as the compiler in your build setup, which explicitly supports transformers.
Install `ttypescript` (`npm install --save-dev ttypescript`), configure your `tsconfig.json` with the transformer plugin, and run your build using `ttsc` instead of `tsc`.
Review the types you are attempting to validate. For types involving functions, use the `functionBehavior` option in `tsconfig.json` to either ignore or perform a simple `typeof` check. Avoid using `typescript-is` for deeply complex, non-serializable type structures.
Ensure `ttypescript` is installed, `tsconfig.json` correctly points to the transformer (`"transform": "typescript-is/lib/transform-inline/transformer"`), and the build command uses `ttsc` instead of `tsc`.
Inspect the input data and the target type definition (e.g., `interface Foo { foo: string; }`) to identify the mismatch. Adjust the data to fit the type or refine the type definition.Configure the `functionBehavior` option in your `tsconfig.json` plugin settings. Options include `ignoreFunctions` (to skip function validation) or `typeof` (to perform a basic `typeof 'function'` check).