Sha256: 36c05e7b3586aefb636d84a555dbec0055c85d3955ff2c388656fb6a914d5830

Contents?: true

Size: 1.19 KB

Versions: 33

Compression:

Stored size: 1.19 KB

Contents

/**
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 {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
```
type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
//=> 'a' | 'c'
```

@category Utilities
*/
export type ConditionalKeys<Base, Condition> = NonNullable<
	// Wrap in `NonNullable` to strip away the `undefined` type from the produced union.
	{
		// 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 since the condition passes.
				? 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

33 entries across 33 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.59 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.58 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.57 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.56 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.55 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.54 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.53 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.52 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.51 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.50 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.49 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.48 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.47 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.46 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.45 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.44 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.43 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.42 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts
immosquare-cleaner-0.1.41 node_modules/crypto-random-string/node_modules/type-fest/source/conditional-keys.d.ts