Skip to content

Commit

Permalink
Add new Required<T, K> as the opposite of Optional<T, K> (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencersutton authored and piotrwitek committed Sep 24, 2019
1 parent cd294f2 commit 495fd1b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Issues can be funded by anyone and the money will be transparently distributed t
* [`OptionalKeys<T>`](#optionalkeyst)
* [`Partial<T>`](#partialt) _(built-in)_
* [`DeepPartial<T>`](#deeppartialt)
* [`Required<T>`](#requiredt) _(built-in)_
* [`Required<T, K>`](#requiredt)
* [`DeepRequired<T>`](#deeprequiredt)
* [`Readonly<T>`](#readonlyt) _(built-in)_
* [`DeepReadonly<T>`](#deepreadonlyt)
Expand Down Expand Up @@ -621,9 +621,22 @@ Make all properties of object type optional

[⇧ back to top](#table-of-contents)

### `Required<T>`
### `Required<T, K>`

Make all properties of object type non-optional
From `T` make a set of properties by key `K` become required

**Usage:**

```ts
import { Required } from 'utility-types';

type Props = { name?: string; age?: number; visible?: boolean; };

// Expect: { name: string; age: number; visible: boolean; }
type Props = Required<Props>
// Expect: { name?: string; age: number; visible: boolean; }
type Props = Required<Props, 'age' | 'visible'>;
```

[⇧ back to top](#table-of-contents)

Expand Down
11 changes: 11 additions & 0 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ exports[`Assign const result: Assign<{}, Omit<T, 'age'>> = rest 1`] = `"any"`;

exports[`Assign testType<Assign<Props, NewProps>>() 1`] = `"Pick<Pick<Props, \\"name\\" | \\"visible\\"> & Pick<NewProps, \\"age\\"> & Pick<NewProps, \\"other\\">, \\"name\\" | \\"age\\" | \\"visible\\" | \\"other\\">"`;

exports[`AugmentedRequired testType<AugmentedRequired<Partial<Props>, 'age' | 'visible'>>({
age: 99,
visible: true,
}) 1`] = `"AugmentedRequired<Partial<Props>, \\"age\\" | \\"visible\\">"`;
exports[`AugmentedRequired testType<AugmentedRequired<Partial<Props>>>({
name: 'Yolo',
age: 99,
visible: true,
}) 1`] = `"AugmentedRequired<Partial<Props>, \\"name\\" | \\"age\\" | \\"visible\\">"`;
exports[`Brand testType<Brand<number, 'USD'>>() 1`] = `"Brand<number, \\"USD\\">"`;
exports[`DeepNonNullable testType<
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export {
Unionize,
ValuesType,
WritableKeys,
AugmentedRequired as Required,
} from './mapped-types';

export * from './type-guards';
Expand Down
17 changes: 17 additions & 0 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
OmitByValueExact,
Optional,
ValuesType,
AugmentedRequired,
} from './mapped-types';

/**
Expand Down Expand Up @@ -527,3 +528,19 @@ type RequiredOptionalProps = {
// @dts-jest:pass:snap -> number
testType<ValuesType<Uint32Array>>();
}

// @dts-jest:group AugmentedRequired
{
// @dts-jest:pass:snap -> AugmentedRequired<Partial<Props>, "name" | "age" | "visible">
testType<AugmentedRequired<Partial<Props>>>({
name: 'Yolo',
age: 99,
visible: true,
});

// @dts-jest:pass:snap -> AugmentedRequired<Partial<Props>, "age" | "visible">
testType<AugmentedRequired<Partial<Props>, 'age' | 'visible'>>({
age: 99,
visible: true,
});
}
17 changes: 17 additions & 0 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
OmitByValueExact,
Optional,
ValuesType,
AugmentedRequired,
} from './mapped-types';

/**
Expand Down Expand Up @@ -527,3 +528,19 @@ type RequiredOptionalProps = {
// @dts-jest:pass:snap
testType<ValuesType<Uint32Array>>();
}

// @dts-jest:group AugmentedRequired
{
// @dts-jest:pass:snap
testType<AugmentedRequired<Partial<Props>>>({
name: 'Yolo',
age: 99,
visible: true,
});

// @dts-jest:pass:snap
testType<AugmentedRequired<Partial<Props>, 'age' | 'visible'>>({
age: 99,
visible: true,
});
}
21 changes: 21 additions & 0 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,24 @@ export type ValuesType<
: T extends object
? T[keyof T]
: never;

/**
* Required
* @desc From `T` make a set of properties by key `K` become required
* @example
* type Props = {
* name?: string;
* age?: number;
* visible?: boolean;
* };
*
* // Expect: { name: string; age: number; visible: boolean; }
* type Props = Required<Props>;
*
* // Expect: { name?: string; age: number; visible: boolean; }
* type Props = Required<Props, 'age' | 'visible'>;
*/
export type AugmentedRequired<
T extends object,
K extends keyof T = keyof T
> = Omit<T, K> & Required<Pick<T, K>>;

0 comments on commit 495fd1b

Please sign in to comment.