GuardedType
Since 1.2.0
Extract the guarded type from a type guard function.
Signature
typescript
type GuardedType<G> = G extends (value: unknown) => value is infer T ? T : neverParameters
| Parameter | Description |
|---|---|
G | The type guard function |
Examples
Basic Usage
typescript
import type { GuardedType } from 'uni-types'
const isNumber = (value: unknown): value is number => typeof value === 'number'
type Num = GuardedType<typeof isNumber> // numberWith Complex Guards
typescript
interface User {
id: string
name: string
}
const isUser = (value: unknown): value is User => {
// guard implementation
}
type UserType = GuardedType<typeof isUser> // UserExtracting from Array
typescript
const guards = [isString, isNumber, isBoolean]
type Types = GuardedType<typeof guards[number]> // string | number | booleanRelated
RuntimeGuard- Define type guard function