Skip to content

Add JustProps and JustMethods types #4

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

Closed
wants to merge 6 commits into from
Closed
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
40 changes: 40 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,43 @@ const ab: Merge<Foo, Bar> = {a: 1, b: 2};
```
*/
export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;

/**
Create a new type from an object type extracting just properties, not methods.
@see JustMethods for extract just methods

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

interface Foo {
a: string;
b: number;
c(): string;
d(x: string): string;
}

const foo: JustProps<Foo> = {a: 'a', b: 1};
```
*/
export type JustProps<ObjectType> = Pick<ObjectType, ({ [Property in keyof ObjectType]: ObjectType[Property] extends (...arguments: unknown[]) => unknown ? never : Property })[keyof ObjectType]>;

/**
Create a new type from an object type extracting just methods, not other properties.
@see JustProps for extract just properties

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

interface Foo {
a: string;
b: number;
c(): string;
d(x: string): string;
}

const foo: JustMethods<Foo> = {c: () => 'c', d: (x: string) => x};
```
*/
export type JustMethods<ObjectType> = Pick<ObjectType, ({ [Method in keyof ObjectType]: ObjectType[Method] extends (...args: unknown[]) => unknown ? Method : never })[keyof ObjectType]>;
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable import/no-unassigned-import */
import './test/omit';
import './test/merge';
import './test/just-props';
import './test/just-methods';

// TODO: Add negative tests. Blocked by: https://github.com/SamVerschueren/tsd-check/issues/2
13 changes: 13 additions & 0 deletions test/just-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {expectType} from 'tsd-check';
import {JustMethods} from '..';

interface Foo {
a: string;
b: number;
c(): string;
d(x: string): string;
}

const foo: JustMethods<Foo> = {c: () => 'c', d: (x: string) => x};

expectType<{c(): string}>(foo);
13 changes: 13 additions & 0 deletions test/just-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {expectType} from 'tsd-check';
import {JustProps} from '..';

interface Foo {
a: string;
b: number;
c(): string;
d(x: string): string;
}

const foo: JustProps<Foo> = {a: 'a', b: 2};

expectType<{a: string; b: number}>(foo);