@m0n0lab/ts-types
v1.0.6
Published
[](https://www.npmjs.com/package/@m0n0lab/ts-types) [](https://codecov.io/gh/pabloimrik17/monolab?fla
Readme
@m0n0lab/ts-types
A centralized package for sharing custom TypeScript type definitions, utilities, and type guards across all Monolab projects.
Features
- 🎯 Centralized type definitions
- ✅ Fully typed with TypeScript
- 📦 Zero runtime dependencies
- ⚡ Lightweight and tree-shakeable
- 🔒 Type-safe and reliable
- 📚 Comprehensive documentation
- 🚀 Production ready library
- 📘 Well documented
Installation
npm
npm install @m0n0lab/ts-typespnpm
pnpm add @m0n0lab/ts-typesJSR
npx jsr add @m0n0lab/ts-typesUsage
Nullable Types
Handle nullable values with type-safe utilities:
import type { Nullable, NonNullable } from "@m0n0lab/ts-types";
import { isNullable, isNonNullable } from "@m0n0lab/ts-types";
// Type definitions
type NullableString = Nullable<string>; // string | null
type RequiredString = NonNullable<string | null>; // string
// Type guards
const value: string | null = getValue();
if (isNonNullable(value)) {
console.log(value.toUpperCase()); // TypeScript knows value is string
}
// Filter arrays
const items: (string | null)[] = ["hello", null, "world"];
const validItems = items.filter(isNonNullable); // string[]Undefinable Types
Work with optional values safely:
import type { Undefinable, NonUndefinable } from "@m0n0lab/ts-types";
import { isUndefinable, isNonUndefinable } from "@m0n0lab/ts-types";
// Type definitions
type OptionalString = Undefinable<string>; // string | undefined
type RequiredString = NonUndefinable<string | undefined>; // string
// Type guards
const value: string | undefined = findValue();
if (isNonUndefinable(value)) {
console.log(value.length); // TypeScript knows value is defined
}Nullish Types
Handle both null and undefined:
import type { Nullish, NonNullish } from "@m0n0lab/ts-types";
import { isNullish, isNonNullish } from "@m0n0lab/ts-types";
// Type definitions
type MaybeString = Nullish<string>; // string | null | undefined
type DefiniteString = NonNullish<string | null | undefined>; // string
// Type guards
const value: string | null | undefined = getOptionalValue();
if (isNonNullish(value)) {
console.log(value.trim()); // TypeScript knows value is string
}
// Filter arrays
const items: (string | null | undefined)[] = [
"hello",
null,
undefined,
"world",
];
const validItems = items.filter(isNonNullish); // string[]StrictOmit
Omit object properties with compile-time safety:
import type { StrictOmit } from "@m0n0lab/ts-types";
interface User {
id: string;
name: string;
email: string;
}
// Valid: 'email' exists in User
type PublicUser = StrictOmit<User, "email">; // { id: string; name: string }
// Error: 'age' doesn't exist in User
// type Invalid = StrictOmit<User, 'age'>; // TypeScript error!
// Unlike built-in Omit which allows non-existent keys:
type Unsafe = Omit<User, "age">; // No error, less safeStringKeyOf
Extract only string keys from object types, filtering out numeric and symbol keys:
import type { StringKeyOf } from "@m0n0lab/ts-types";
interface MixedKeys {
name: string; // string key
age: number; // string key
42: string; // numeric key
[Symbol.iterator]: () => void; // symbol key
}
// Only extracts string keys
type OnlyStringKeys = StringKeyOf<MixedKeys>; // "name" | "age"
// Useful for type-safe object key operations
interface Config {
host: string;
port: number;
debug: boolean;
}
type ConfigKey = StringKeyOf<Config>; // "host" | "port" | "debug"
// Works with Record types
type StringRecord = Record<string, number>;
type RecordKeys = StringKeyOf<StringRecord>; // string
// Returns never for objects with no string keys
interface OnlyNumeric {
0: string;
1: number;
}
type NoKeys = StringKeyOf<OnlyNumeric>; // neverContributing
Contributions are welcome! Please read our contributing guidelines before submitting a pull request.
License
MIT
