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

Add JustProps and JustMethods types #4

Closed
wants to merge 6 commits into from
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
tonivj5 marked this conversation as resolved.
Show resolved Hide resolved

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
@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]>;
tonivj5 marked this conversation as resolved.
Show resolved Hide resolved

/**
Create a new type from an object type extracting just methods
tonivj5 marked this conversation as resolved.
Show resolved Hide resolved

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