/ts-type-predicate-example

An example of how to use TypeScript type predicates (formerly called type guards) to validate object shapes.

Primary LanguageTypeScriptMIT LicenseMIT

Illustrating TypeScript Type Predicates ✨

A simple test suite for illustrating how to use TS type predicates and descriptor objects to validate input against a type at runtime.

Getting Started

Clone the repo, then:

npm install
npm run test

Caveats

The instanceOf type predicate function contained within is extremely basic and provides only a loose form of validation. Since object property types are only validated using JavaScript's typeof operator, it doesn't check for validity of things like string literals, nested object shapes, etc.

The primary purpose of this example is to illustrate how a type predicate might be used to validate an object type with several distinct members and how TS uses the predicate to provide type hinting in your code.

If you want to use this function in production, you should modify it to provide stricter validation for your specific use case.

License

This source code is licensed under the MIT license.