typeGuard
Aims at making the most of the value is T statement.
Implementation:
export function typeGuard<T>(value: any, isMatched: boolean): value is T {
return isMatched;
}Use case 1: Tell the compiler what assertion can be made on a given variable if a given test returns true.
import { typeGuard } from "tsafe/typeGuard";
type Circle = { type: "CIRCLE"; radius: number };
type Square = { type: "SQUARE"; sideLength: number };
type Shape = Circle | Square;
declare const shape: Shape;
if (typeGuard<Circle>(shape, shape.type.startsWith("C"))) {
//The developer knows the shape is is a CIRCLE,
//TypeScript can't tell but trusts the developer.
shape.radius;
} else {
shape.sideLength;
}Usage in conjonction with assert:
Use case 2: Helper for safely build other type guards
Last updated
Was this helpful?