TypeScript Utilities
This is a collection of TypeScript utilities that we reuse across all TS projects.
Installation
Using Yarn
yarn add @myparcel/ts-utils
Using pnpm
pnpm add @myparcel/ts-utils
Using npm
npm install @myparcel/ts-utils
⚠️ Note: You can install this package as a dev dependency if you are not using the type guards.
Contents
Type guards
Type guard for checking if a value is a key of the given enum.
import { isEnumValue } from '@myparcel/ts-utils';
enum MyEnum {
A = 'A',
B = 'B',
}
const value = 'A';
if (isEnumValue(MyEnum, value)) {
}
Type guard for checking if an object value is of a specific type by checking if a given K exists.
import { isOfType } from '@myparcel/ts-utils';
interface BaseObject {
a: string;
b: number;
}
interface ObjectWithC extends BaseObject {
c: string;
}
const value = {
a: 'a',
b: 1,
c: 'c',
};
if (isOfType<ObjectWithC>(value, 'c')) {
}
Types
Utils