Skip to content

Instantly share code, notes, and snippets.

View kasper573's full-sized avatar

Kasper kasper573

View GitHub Profile
@webstrand
webstrand / 000~nonempty-validator.ts
Last active June 20, 2023 16:37
Examples of the <T>(foo: Validator<T>) pattern in typescript
/******************************************************************************
* Implementation of `Nonempty` validator which checks that the provided type
* has at least one defined property, excluding `{}`.
******************************************************************************/
type Nonempty<T extends { [key: string]: any }> = { [P in keyof T]: T }[keyof T];
declare function wantsNonempty<T extends { [key: string]: any }>(x: Nonempty<T>): true;
wantsNonempty({ x: 1 });
wantsNonempty({}); // error expected