If you've worked with TypeScript for a while, you know that sometimes you obtain any or unknown data that is not type-safe. You'd then have to write y

Search code, repositories, users, issues, pull requests...

submited by
Style Pass
2024-06-10 17:00:09

If you've worked with TypeScript for a while, you know that sometimes you obtain any or unknown data that is not type-safe. You'd then have to write your own function with type predicates that checks the foreign object, and makes sure it is the type that you need.

At compile time, it inspects the type you want to have checked, and generates a function that can check the type of a wild object at run-time. When the function is invoked, it checks in detail if the given wild object complies with your favorite type.

NOTE this package aims to generate type predicates for any serializable JavaScript object. Please check What it won't do for details.

As there currently is no way to configure the TypeScript compiler to use a transformer without using it programatically, the recommended way is to compile with ttypescript. This is basically a wrapper around the TypeScript compiler that injects transformers configured in your tsconfig.json.

If you are using ts-loader in a webpack project, you can use getCustomTransformers as suggested in #54. This means you don't need to use ttypescript or write a custom compilation script.

Leave a Comment