diff --git a/index.d.ts b/index.d.ts index 562ebbc59..598ed89e5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -84,3 +84,43 @@ const ab: Merge = {a: 1, b: 2}; ``` */ export type Merge = Omit> & 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 = {a: 'a', b: 1}; +``` +*/ +export type JustProps = Pick 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 = {c: () => 'c', d: (x: string) => x}; +``` +*/ +export type JustMethods = Pick unknown ? Method : never })[keyof ObjectType]>; diff --git a/index.test-d.ts b/index.test-d.ts index a7e927bbb..ded54ea36 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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 diff --git a/test/just-methods.ts b/test/just-methods.ts new file mode 100644 index 000000000..21f8f0f80 --- /dev/null +++ b/test/just-methods.ts @@ -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 = {c: () => 'c', d: (x: string) => x}; + +expectType<{c(): string}>(foo); diff --git a/test/just-props.ts b/test/just-props.ts new file mode 100644 index 000000000..0a0fdaf0c --- /dev/null +++ b/test/just-props.ts @@ -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 = {a: 'a', b: 2}; + +expectType<{a: string; b: number}>(foo);