RequireKeys type
Makes some optional properties required: the result of filling in defaults.
ts
import type { RequireKeys } from 'utils';Signature
ts
export type RequireKeys<T, K extends keyof T> = Simplify<Omit<T, K> & Required<Pick<T, K>>>;Parameters
| Name | Description |
|---|---|
T | The object type. |
K | The keys to require. |
Example
ts
type ResolvedOptions = RequireKeys<Options, 'duration' | 'easing'>;Tests
Line coverage: 0 %.
- ✅ requires the given keys only
Source
src/types/require-keys.ts (on GitHub)
Uses
Simplify: Flattens intersections and mapped types into a plain object type, so that editor tooltips show the properties instead ofA & Omit<B, 'c'>.
Files it needs
src/types/require-keys.ts imports, directly or not:
Source code
ts
import type { Simplify } from './simplify';
/**
* Makes some optional properties required: the result of filling in defaults.
*
* @template T - The object type.
* @template K - The keys to require.
* @example
* type ResolvedOptions = RequireKeys<Options, 'duration' | 'easing'>;
*/
export type RequireKeys<T, K extends keyof T> = Simplify<Omit<T, K> & Required<Pick<T, K>>>;Test file
ts
import type { RequireKeys } from './require-keys';
describe('RequireKeys', () => {
it('requires the given keys only', () => {
expectTypeOf<RequireKeys<{ a?: number; b?: string }, 'a'>>().toEqualTypeOf<{ a: number; b?: string }>();
});
});Keywords: require, keys, types, type