Skip to content

cast

function cast<T>(value): T;

Defined in: cast.ts:19

Return value typed as T. The same as value as T: an unchecked type assertion with no runtime effect. As a function call it fits in expression chains and is easy to search for.

Nothing is validated, so only use it when you know the shape. When the data might not match, use a schema or a type guard instead.

Type Parameter
T
Parameter Type
value unknown

T

import { cast } from "@xtandard/lib";
type AppConfig = { port: number };
const config = cast<AppConfig>(JSON.parse('{"port":8080}'));
config.port; // 8080 (config: AppConfig, not any)