Sha256: eb31477c87de3309cbe4e9984fa74a052f31581edb89103f8590f01874b4e271

Contents?: true

Size: 1.17 KB

Versions: 28

Compression:

Stored size: 1.17 KB

Contents

import type {RequiredKeysOf} from './required-keys-of';

/**
Creates a type that represents `true` or `false` depending on whether the given type has any required fields.

This is useful when you want to create an API whose behavior depends on the presence or absence of required fields.

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

type GeneratorOptions<Template extends object> = {
	prop1: number;
	prop2: string;
} & (HasRequiredKeys<Template> extends true
	? {template: Template}
	: {template?: Template});

interface Template1 {
	optionalSubParam?: string;
}

interface Template2 {
	requiredSubParam: string;
}

type Options1 = GeneratorOptions<Template1>;
type Options2 = GeneratorOptions<Template2>;

const optA: Options1 = {
	prop1: 0,
	prop2: 'hi'
};
const optB: Options1 = {
	prop1: 0,
	prop2: 'hi',
	template: {}
};
const optC: Options1 = {
	prop1: 0,
	prop2: 'hi',
	template: {
		optionalSubParam: 'optional value'
	}
};

const optD: Options2 = {
	prop1: 0,
	prop2: 'hi',
	template: {
		requiredSubParam: 'required value'
	}
};

```

@category Utilities
*/
export type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;

Version data entries

28 entries across 28 versions & 2 rubygems

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