UNPKG

658 BTypeScriptView Raw
1import type {IfEmptyObject} from './if-empty-object';
2import type {IsUnion} from './internal';
3
4/**
5Create a type that only accepts an object with a single key.
6
7@example
8```
9import type {SingleKeyObject} from 'type-fest';
10
11const someFunction = <T>(parameter: SingleKeyObject<T>) => {};
12
13someFunction({
14 value: true
15});
16
17someFunction({
18 value: true,
19 otherKey: true
20});
21// Error: Argument of type '{value: boolean; otherKey: boolean}' is not assignable to parameter of type 'never'.ts(2345)
22```
23
24@category Object
25*/
26export type SingleKeyObject<ObjectType> =
27 IsUnion<keyof ObjectType> extends true
28 ? never
29 : IfEmptyObject<ObjectType, never, ObjectType>;