From 92065d13b6f339edec7c034b81e5964d106295a7 Mon Sep 17 00:00:00 2001 From: xxxtonixxx Date: Wed, 13 Mar 2019 17:53:30 +0100 Subject: [PATCH 1/6] feat: add JustProps and JustMethods types --- index.d.ts | 38 ++++++++++++++++++++++++++++++++++++++ index.test-d.ts | 2 ++ test/just-methods.ts | 13 +++++++++++++ test/just-props.ts | 13 +++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 test/just-methods.ts create mode 100644 test/just-props.ts diff --git a/index.d.ts b/index.d.ts index 562ebbc59..4ad54ff19 100644 --- a/index.d.ts +++ b/index.d.ts @@ -84,3 +84,41 @@ const ab: Merge = {a: 1, b: 2}; ``` */ export type Merge = Omit> & 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 = {a: 'a', b: 1}; +``` +*/ +export type JustProps = Pick unknown ? never : Property })[keyof ObjectType]>; + +/** +Create a new type from an object type extracting just methods + +@example +``` +import {JustProps} 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); From 448e71c72c420b04d4fa94fe7377479e41dba283 Mon Sep 17 00:00:00 2001 From: xxxtonixxx Date: Wed, 13 Mar 2019 17:54:37 +0100 Subject: [PATCH 2/6] fix: example --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 4ad54ff19..bf6c31b4a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -109,7 +109,7 @@ Create a new type from an object type extracting just methods @example ``` -import {JustProps} from 'type-fest'; +import {JustMethods} from 'type-fest'; interface Foo { a: string; From b19f0df46869f0392d3b8ee46d8dc1148dbeb66c Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 13 Mar 2019 22:49:53 +0100 Subject: [PATCH 3/6] fix: just-props description Co-Authored-By: xxxtonixxx --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index bf6c31b4a..6d46ee736 100644 --- a/index.d.ts +++ b/index.d.ts @@ -86,7 +86,7 @@ const ab: Merge = {a: 1, b: 2}; export type Merge = Omit> & SecondType; /** -Create a new type from an object type extracting just props +Create a new type from an object type extracting just properties, not methods. @example ``` From 32ac354595cf6be3eb732662ac6e3dbe965a5018 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 13 Mar 2019 22:50:12 +0100 Subject: [PATCH 4/6] fix: just-methods description Co-Authored-By: xxxtonixxx --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 6d46ee736..9c1769c61 100644 --- a/index.d.ts +++ b/index.d.ts @@ -105,7 +105,7 @@ 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 +Create a new type from an object type extracting just methods, not other properties. @example ``` From e90c0cd4c7d6638cc4c188736943a2c562d9dab5 Mon Sep 17 00:00:00 2001 From: xxxtonixxx Date: Wed, 13 Mar 2019 23:49:46 +0100 Subject: [PATCH 5/6] use @see tag --- index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.d.ts b/index.d.ts index 9c1769c61..d00c7ca81 100644 --- a/index.d.ts +++ b/index.d.ts @@ -87,6 +87,7 @@ export type Merge = Omit = Pick Date: Wed, 13 Mar 2019 23:50:32 +0100 Subject: [PATCH 6/6] rename args to arguments Co-Authored-By: xxxtonixxx --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index d00c7ca81..598ed89e5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -103,7 +103,7 @@ interface Foo { const foo: JustProps = {a: 'a', b: 1}; ``` */ -export type JustProps = Pick unknown ? never : Property })[keyof ObjectType]>; +export type JustProps = Pick unknown ? never : Property })[keyof ObjectType]>; /** Create a new type from an object type extracting just methods, not other properties.