refactor: use strict typing

This commit is contained in:
Carlos Valente
2025-03-21 19:44:16 +01:00
committed by arc-alex
parent 14a7c04d3b
commit d48b2ae506
39 changed files with 339 additions and 246 deletions
+9 -9
View File
@@ -1,29 +1,31 @@
import { is } from './is.js';
export function isString(value: unknown): asserts value is string {
if (typeof value !== 'string') {
if (!is.string(value)) {
throw new Error(`Unexpected payload type: ${String(value)}`);
}
}
export function isDefined<T>(value: T | undefined): asserts value is T {
if (value === undefined) {
if (!is.defined(value)) {
throw new Error('Payload not found');
}
}
export function isNumber(value: unknown): asserts value is number {
if (typeof value !== 'number') {
if (!is.number(value)) {
throw new Error(`Unexpected payload type: ${String(value)}`);
}
}
export function isObject(value: unknown): asserts value is object {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
if (!is.object(value)) {
throw new Error(`Unexpected payload type: ${String(value)}`);
}
}
export function isArray(value: unknown): asserts value is unknown[] {
if (!Array.isArray(value)) {
if (!is.array(value)) {
throw new Error(`Unexpected payload type: ${String(value)}`);
}
}
@@ -32,9 +34,7 @@ export function hasKeys<T extends object, K extends keyof any>(
value: T,
keys: K[],
): asserts value is T & Record<K, unknown> {
for (const key of keys) {
if (!(key in value)) {
throw new Error(`Key not found: ${String(key)}`);
}
if (!is.objectWithKeys(value, keys)) {
throw new Error(`Unexpected payload type: ${String(value)}`);
}
}