remeda
Version:
A utility library for JavaScript and Typescript.
13 lines (10 loc) • 410 B
text/typescript
import { IsAny } from 'type-fest';
/**
* An extension of Extract for type predicates which falls back to the base
* in order to narrow the `unknown` case.
*
* @example
* function isMyType<T>(data: T | MyType): data is NarrowedTo<T, MyType> { ... }
*/
type NarrowedTo<T, Base> = Extract<T, Base> extends never ? Base : IsAny<T> extends true ? Base : Extract<T, Base>;
export type { NarrowedTo as N };