import type {RequireAllOrNone} from 'type-fest';
type Responder = {
text?: () => string;
json?: () => string;
secure: boolean;
};
const responder1: RequireAllOrNone<Responder, 'text' | 'json'> = {
secure: true
};
const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = {
text: () => '{"message": "hi"}',
json: () => '{"message": "ok"}',
secure: true
};
Create a type that requires all of the given keys or none of the given keys. The remaining keys are kept as is.
Use-cases:
The caveat with
RequireAllOrNoneis that TypeScript doesn't always know at compile time every key that will exist at runtime. ThereforeRequireAllOrNonecan't do anything to prevent extra keys it doesn't know about.