Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added WritableKeys/ReadonlyKeys mapped types #61

Merged
merged 3 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ This gives you the power to prioritize our work and support project contributors
* [`Subtract<T, T1>`](#subtractt-t1)
* [`Overwrite<T, U>`](#overwritet-u)
* [`Assign<T, U>`](#assignt-u)
* [`ReadonlyKeys<T>`](#readonlykeyst)
* [`WritableKeys<T>`](#writablekeyst)

## Mapped Types

Expand Down Expand Up @@ -399,6 +401,38 @@ type ExtendedProps = Assign<Props, NewProps>;

[⇧ back to top](#operations-on-objects)

### `ReadonlyKeys<T>`

Get union type of keys that are readonly in object type `T`

**Usage:**

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

type Props = { readonly foo: string; bar: number };
type ReadonlyProps = ReadonlyKeys<Props>;
// Expect: "foo"
```

[⇧ back to top](#operations-on-objects)

### `WritableKeys<T>`

Get union type of keys that are writable (not readonly) in object type `T`

**Usage:**

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

type Props = { readonly foo: string; bar: number };
type WritableProps = WritableKeys<Props>;
// Expect: "bar"
```

[⇧ back to top](#operations-on-objects)

---

## Mapped Types
Expand Down
4 changes: 4 additions & 0 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ exports[`PickByValue testType<PickByValue<Props, string | number>>() 1`] = `"Pic

exports[`PromiseType testType<PromiseType<Promise<string>>>() 1`] = `"string"`;

exports[`ReadonlyKeys testType<ReadonlyKeys<ReadWriteProps>>() 1`] = `"\\"a\\""`;

exports[`SetComplement testType<SetComplement<'1' | '2' | '3', '2' | '3'>>() 1`] = `"\\"1\\""`;

exports[`SetDifference testType<SetDifference<'1' | '2' | '3', '2' | '3' | '4'>>() 1`] = `"\\"1\\""`;
Expand All @@ -119,3 +121,5 @@ exports[`Subtract testType<Subtract<Props, DefaultProps>>() 1`] = `"Pick<Props,
exports[`SymmetricDifference testType<SymmetricDifference<'1' | '2' | '3', '2' | '3' | '4'>>() 1`] = `"\\"1\\" | \\"4\\""`;

exports[`Unionize testType<Unionize<Props>>() 1`] = `"{ name: string; } | { age: number; } | { visible: boolean; }"`;

exports[`WritableKeys testType<WritableKeys<ReadWriteProps>>() 1`] = `"\\"b\\""`;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export {
Subtract,
SymmetricDifference,
Unionize,
WritableKeys,
ReadonlyKeys,
} from './mapped-types';

// deprecated
Expand Down
15 changes: 15 additions & 0 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
DeepRequired,
DeepNonNullable,
DeepPartial,
WritableKeys,
ReadonlyKeys,
_DeepNonNullableArray,
_DeepNonNullableObject,
_DeepReadonlyArray,
Expand All @@ -39,6 +41,7 @@ type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };
type NewProps = { age: string; other: string };
type MixedProps = { name: string; setName: (name: string) => void };
type ReadWriteProps = { readonly a: number; b: string };

/**
* Tests
Expand Down Expand Up @@ -337,3 +340,15 @@ it('DeepPartial', () => {
// @dts-jest:pass:snap -> string
testType<ReturnType<NonNullable<typeof functionProp>>>();
});

// @dts-jest:group WritableKeys
it('WritableKeys', () => {
// @dts-jest:pass:snap -> "b"
testType<WritableKeys<ReadWriteProps>>();
});

// @dts-jest:group ReadonlyKeys
it('ReadonlyKeys', () => {
// @dts-jest:pass:snap -> "a"
testType<ReadonlyKeys<ReadWriteProps>>();
});
15 changes: 15 additions & 0 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
DeepRequired,
DeepNonNullable,
DeepPartial,
WritableKeys,
ReadonlyKeys,
_DeepNonNullableArray,
_DeepNonNullableObject,
_DeepReadonlyArray,
Expand All @@ -39,6 +41,7 @@ type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };
type NewProps = { age: string; other: string };
type MixedProps = { name: string; setName: (name: string) => void };
type ReadWriteProps = { readonly a: number; b: string };

/**
* Tests
Expand Down Expand Up @@ -337,3 +340,15 @@ it('DeepPartial', () => {
// @dts-jest:pass:snap
testType<ReturnType<NonNullable<typeof functionProp>>>();
});

// @dts-jest:group WritableKeys
it('WritableKeys', () => {
// @dts-jest:pass:snap
testType<WritableKeys<ReadWriteProps>>();
});

// @dts-jest:group ReadonlyKeys
it('ReadonlyKeys', () => {
// @dts-jest:pass:snap
testType<ReadonlyKeys<ReadWriteProps>>();
});
24 changes: 24 additions & 0 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,27 @@ export interface _DeepPartialArray<T> extends Array<DeepPartial<T>> { }
export type _DeepPartialObject<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};

type IfEquals<X, Y, A = X, B = never> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? A : B;

/**
* WritableKeys
* @desc get union type of keys that are writable in object type `T`
* Credit: Matt McCutchen
* https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript
*/
export type WritableKeys<T extends object> = {
[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, P>
}[keyof T];

/**
* ReadonlyKeys
* @desc get union type of keys that are readonly in object type `T`
* Credit: Matt McCutchen
* https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript
*/
export type ReadonlyKeys<T extends object> = {
[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>
}[keyof T];