https://github.com/michaeljscript/create-typeguard
Simple package for creating safe type guards for TypeScript
https://github.com/michaeljscript/create-typeguard
typeguard typescript
Last synced: 11 months ago
JSON representation
Simple package for creating safe type guards for TypeScript
- Host: GitHub
- URL: https://github.com/michaeljscript/create-typeguard
- Owner: michaeljscript
- License: mit
- Created: 2019-04-02T11:33:08.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T18:58:57.000Z (about 3 years ago)
- Last Synced: 2025-02-11T05:17:02.836Z (12 months ago)
- Topics: typeguard, typescript
- Language: TypeScript
- Homepage: https://medium.com/@michalszorad/typescript-keeping-type-guards-safe-and-up-to-date-2457d52bd722
- Size: 598 KB
- Stars: 14
- Watchers: 1
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Create Type Guard
Simple package for creating type-safe type guards for TypeScript.
## What problem is being solved?
```ts
interface Person {
age: number;
name: string;
}
function isPerson(data: unknown): data is Person {
return typeof data === 'object' && data && Object.hasOwnProperty.call(data, 'name');
}
// Oooops, you forgot to check if the `age` property does exist in `data`.
// Oooops, you forgot to check the type of the `name` property. Is it a string? Is it a number?
// Oooops, you forgot to check the type of the `age` property. Is it a string? Is it a number?
// With create-typeguard this won't happen.
```
## How does it work?
This package creates type-safe type guards from a parsers, because parsers will warn us if we make a mistake!
## Usage
```ts
import { createTypeGuard, hasProperties } from "create-typeguard";
interface Person {
age: number;
name: string;
}
// Our parser function that is type-safe
function parsePerson(data: unknown): Person | null {
if (typeof data === 'object' && data && hasProperties(data, 'name', 'age')) {
const { name, age } = data;
if (typeof name === 'string' && typeof age === 'number') {
return { name, age };
}
}
return null;
}
// This is now a type-safe typeguard for Person!
const isPerson = createTypeGuard(parsePerson);
const maybePerson: unknown = response.body;
if (isPerson(maybePerson)) {
// maybePerson is Person
console.log(`${maybePerson.name} is ${maybePerson.age} years old.`);
}
```
## Usage 2 - Without defining a separate parse function
```ts
import { createTypeGuard, hasProperties } from "create-typeguard";
interface Person {
age: number;
name: string;
}
// You do not have to create a separate parse function.
const isPerson = createTypeGuard(data => {
if (typeof data === 'object' && data && hasProperties(data, 'name', 'age')) {
const { name, age } = data;
if (typeof name === 'string' && typeof age === 'number') {
return { name, age };
}
}
return null;
});
const maybePerson: unknown = response.body;
if (isPerson(maybePerson)) {
// maybePerson is Person
console.log(`${maybePerson.name} is ${maybePerson.age} years old.`);
}
```
## Why create-typeguard?
Read more at this blog post https://medium.com/@michalszorad/typescript-keeping-type-guards-safe-and-up-to-date-2457d52bd722