Skip to main content

Function: validateAndConvert()

validateAndConvert<T>(value: unknown, validator: (val: unknown) => val is T, errorMessage?: string): T

Defined in: shared/utils/typeHelpers.ts:105

Type-safe conversion from unknown to a specific type with validation.

Type Parametersโ€‹

Tโ€‹

T

Parametersโ€‹

valueโ€‹

unknown

Unknown value to convert

validatorโ€‹

(val: unknown) => val is T

Validation function that returns true if value is of type T

errorMessage?โ€‹

string

Returnsโ€‹

T

Validated value of type T

Remarksโ€‹

Use this instead of direct type assertions when you can validate the type. Provides runtime safety in addition to compile-time types.

Throwsโ€‹

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Error if validation fails