UNPKG

type-fest

Version:

A collection of essential TypeScript types

38 lines (27 loc) • 1.16 kB
import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts'; /** Matches any non-empty string. This is useful when you need a string that is not empty, for example, as a function parameter. NOTE: - This returns `never` not just when instantiated with an empty string, but also when an empty string is a subtype of the underlying string type, like `string` or `Uppercase<string>`. @example ``` import type {NonEmptyString} from 'type-fest'; declare function foo<T extends string>(string: NonEmptyString<T>): void; foo('a'); // OK // @ts-expect-error foo(''); // Error: Argument of type '""' is not assignable to parameter of type 'never'. declare const someString: string; // @ts-expect-error foo(someString); // Error: Argument of type 'string' is not assignable to parameter of type 'never'. ``` @category String */ // The string form also checks enum values; keep the unwrapped type so the constraint remains strict for generic strings. export type NonEmptyString<T extends string> = (T extends object ? UnwrapBrandedPrimitive<T> : T) extends infer Input extends string ? '' extends Input | `${Input}` ? never : T : never; export {};