From bdaefe5b4a629891b1857f3d66191bcd2979d49d Mon Sep 17 00:00:00 2001 From: wujingtao <601595686@qq.com> Date: Wed, 22 Aug 2018 22:46:10 +0800 Subject: [PATCH] optimizing type definition --- types/pidusage/index.d.ts | 14 +++++--------- types/pidusage/pidusage-tests.ts | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/types/pidusage/index.d.ts b/types/pidusage/index.d.ts index a883387a67..d21fca3d7f 100644 --- a/types/pidusage/index.d.ts +++ b/types/pidusage/index.d.ts @@ -3,16 +3,12 @@ // Definitions by: Cyril Schumacher // wujingtao // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 -/** - * @param pids A pid or a list of pids. - * @param callback Called when the statistics are ready. - */ -declare function pidusage(pids: number | number[] | string | string[], callback: (err: Error | null, stats: pidusage.Stat) => void): void; -/** - * @param pids A pid or a list of pids. - */ -declare function pidusage(pids: number | number[] | string | string[]): Promise; +declare function pidusage(pid: number | string, callback: (err: Error | null, stats: pidusage.Stat) => void): void; +declare function pidusage(pids: Array, callback: (err: Error | null, stats: { [key: string]: pidusage.Stat }) => void): void; +declare function pidusage(pid: number | string): Promise; +declare function pidusage(pids: Array): Promise<{ [key: string]: pidusage.Stat }>; declare namespace pidusage { interface Stat { diff --git a/types/pidusage/pidusage-tests.ts b/types/pidusage/pidusage-tests.ts index b6835cff18..37b5715eed 100644 --- a/types/pidusage/pidusage-tests.ts +++ b/types/pidusage/pidusage-tests.ts @@ -18,8 +18,21 @@ pidusage(1, (err: Error | null, stats: pidusage.Stat) => { timestamp = stats.timestamp; }); -let stats: Promise; +pidusage('two', (err: Error | null, stats: pidusage.Stat) => { + cpu = stats.cpu; + memory = stats.memory; + ppid = stats.ppid; + pid = stats.pid; + ctime = stats.ctime; + elapsed = stats.elapsed; + timestamp = stats.timestamp; +}); -stats = pidusage([1, 2]); -stats = pidusage('string'); -stats = pidusage(['string', 'string']); +pidusage([1, 'two'], (err: Error | null, stats: { [key: string]: pidusage.Stat }) => { + const one: pidusage.Stat = stats[1]; + const two: pidusage.Stat = stats.two; +}); + +const stats_1: Promise = pidusage(1); +const stats_two: Promise = pidusage('two'); +const stats_obj: Promise<{ [key: string]: pidusage.Stat }> = pidusage([1, 'two']);