Sha256: b848b40bfeb73dfe2e782c5b7588ef521010a3d595297e69386670cbde6b4d82

Contents?: true

Size: 1.28 KB

Versions: 28

Compression:

Stored size: 1.28 KB

Contents

import type {IfNever} from './if-never';

/**
Extract the keys from a type where the value type of the key extends the given `Condition`.

Internally this is used for the `ConditionalPick` and `ConditionalExcept` types.

@example
```
import type {ConditionalKeys} from 'type-fest';

interface Example {
	a: string;
	b: string | number;
	c?: string;
	d: {};
}

type StringKeysOnly = ConditionalKeys<Example, string>;
//=> 'a'
```

To support partial types, make sure your `Condition` is a union of undefined (for example, `string | undefined`) as demonstrated below.

@example
```
import type {ConditionalKeys} from 'type-fest';

type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
//=> 'a' | 'c'
```

@category Object
*/
export type ConditionalKeys<Base, Condition> =
{
	// Map through all the keys of the given base type.
	[Key in keyof Base]-?:
	// Pick only keys with types extending the given `Condition` type.
	Base[Key] extends Condition
	// Retain this key
	// If the value for the key extends never, only include it if `Condition` also extends never
		? IfNever<Base[Key], IfNever<Condition, Key, never>, Key>
	// Discard this key since the condition fails.
		: never;
	// Convert the produced object into a union type of the keys which passed the conditional test.
}[keyof Base];

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/type-fest/source/conditional-keys.d.ts