Validate data at runtime using TypeScript types
npm install ts-validate-typeValidate data at runtime using TypeScript types.
``ts`
const user = validateType<{ name: string }>(data);
Building apps in TypeScript often requires handling data we don't know the type
of at compile time. For example fetching data:
`ts`
const myData = fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(response => response.json())
.then(response => {
// casting is unsafe!
return response as {
userId: number;
id: number;
title: string;
completed: boolean;
};
});
With ts-validate-type:
`ts`
const myData = fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(response => response.json())
.then(response => {
// throws error if response and type don't match
return validateType<{
userId: number;
id: number;
title: string;
completed: boolean;
}>(response);
});
ts-validate-type requires a compile-time plugin to work. Given this example:
`ts`
validateType
The plugin will add the type parameter as a runtime argument (aTsValidateType
stringified object of type ),
which is then used for validation:
`ts`
validateType
> Note: currently only a TypeScript setup using Babel is supported.
1. Install dependencies
`bash`
npm install ts-validate-type
`
bash`
npm install --save-dev babel-plugin-ts-validate-type
`
2. Add Babel plugin
json`
"plugins": ["babel-plugin-ts-validate-type", ...]
`ts`
function validateType
Checks the type of the value passed in matches the type argument T. Throwsvalue
error if the types don't match, otherwise returns the passed in.
T must be an inline type. Using type variables like is not
supported.
#### Examples
`ts
import { validateType } from "ts-validate-type";
try {
const myValue = validateType
} catch (e) {
// handle incorrect type
}
`
`ts``
validateType<{
version: "1";
name: string;
id: string | number;
tags: string[];
nested: { value: boolean };
tuple: [bigint, bigint];
data: any;
}>(data);