Sha256: 952c4a8d2338e19ef26c1c0758815b1de6c082a485f88368f5bece1e555f39d4

Contents?: true

Size: 1.08 KB

Versions: 28

Compression:

Stored size: 1.08 KB

Contents

import type {Simplify} from './simplify';

// Returns `never` if the key is optional otherwise return the key type.
type RequiredFilter<Type, Key extends keyof Type> = undefined extends Type[Key]
	? Type[Key] extends undefined
		? Key
		: never
	: Key;

// Returns `never` if the key is required otherwise return the key type.
type OptionalFilter<Type, Key extends keyof Type> = undefined extends Type[Key]
	? Type[Key] extends undefined
		? never
		: Key
	: never;

/**
Enforce optional keys (by adding the `?` operator) for keys that have a union with `undefined`.

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

type Foo = {
	a: string;
	b?: string;
	c: undefined;
	d: number | undefined;
};

type FooBar = EnforceOptional<Foo>;
// => {
// 	a: string;
// 	b?: string;
// 	c: undefined;
// 	d?: number;
// }
```

@internal
@category Object
*/
export type EnforceOptional<ObjectType> = Simplify<{
	[Key in keyof ObjectType as RequiredFilter<ObjectType, Key>]: ObjectType[Key]
} & {
	[Key in keyof ObjectType as OptionalFilter<ObjectType, Key>]?: Exclude<ObjectType[Key], undefined>
}>;

Version data entries

28 entries across 28 versions & 2 rubygems

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