Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
38 changes: 38 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,41 @@ 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 props

@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 (...args: unknown[]) => unknown ? never : Property })[keyof ObjectType]>;

/**
Create a new type from an object type extracting just methods

@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);