react-querybuilder
Version:
React Query Builder component for constructing queries and filters, with utilities for executing them in various database and evaluation contexts
62 lines (52 loc) • 2.1 kB
text/typescript
import type { IsAny } from "../is-any.mjs";
import type { IsLiteral } from "../is-literal.mjs";
import type { ToString } from "./string.mjs";
type BaseKeyFilter<
Type,
Key extends keyof Type
> = Key extends symbol ? never : Type[Key] extends symbol ? never : Type[Key] extends Record<string, unknown> ? Key : [(...arguments_: any[]) => any] extends [Type[Key]] ? never : Key;
/**
Returns the required keys.
@group type-fest
*/
export type FilterDefinedKeys<T extends object> = Exclude<{ [Key in keyof T] : IsAny<T[Key]> extends true ? Key : undefined extends T[Key] ? never : T[Key] extends undefined ? never : BaseKeyFilter<T, Key> }[keyof T], undefined>;
/**
Returns the optional keys.
@group type-fest
*/
export type FilterOptionalKeys<T extends object> = Exclude<{ [Key in keyof T] : IsAny<T[Key]> extends true ? never : undefined extends T[Key] ? T[Key] extends undefined ? never : BaseKeyFilter<T, Key> : never }[keyof T], undefined>;
/**
Disallows any of the given keys.
@group type-fest
*/
export type RequireNone<KeysType extends PropertyKey> = Partial<Record<KeysType, never>>;
/**
Utility type to retrieve only literal keys from type.
@group type-fest
*/
export type LiteralKeyOf<T> = keyof { [K in keyof T as IsLiteral<K> extends true ? K : never]-? : never };
/**
Get the exact version of the given `Key` in the given object `T`.
Use-case: You known that a number key (e.g. 10) is in an object, but you don't know how it is defined in the object, as a string or as a number (e.g. 10 or '10'). You can use this type to get the exact version of the key. See the example.
@example
```
type Object = {
0: number;
'1': string;
};
type Key1 = ExactKey<Object, '0'>;
//=> 0
type Key2 = ExactKey<Object, 0>;
//=> 0
type Key3 = ExactKey<Object, '1'>;
//=> '1'
type Key4 = ExactKey<Object, 1>;
//=> '1'
```
@group type-fest
*/
export type ExactKey<
T extends object,
Key extends PropertyKey
> = Key extends keyof T ? Key : ToString<Key> extends keyof T ? ToString<Key> : Key extends `${infer NumberKey extends number}` ? NumberKey extends keyof T ? NumberKey : never : never;
export {};