optimizing type definition

This commit is contained in:
wujingtao
2018-08-22 22:46:10 +08:00
parent 9d472aa6e9
commit bdaefe5b4a
2 changed files with 22 additions and 13 deletions
+5 -9
View File
@@ -3,16 +3,12 @@
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>
// wujingtao <https://github.com/mx601595686>
// 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<pidusage.Stat>;
declare function pidusage(pid: number | string, callback: (err: Error | null, stats: pidusage.Stat) => void): void;
declare function pidusage(pids: Array<number | string>, callback: (err: Error | null, stats: { [key: string]: pidusage.Stat }) => void): void;
declare function pidusage(pid: number | string): Promise<pidusage.Stat>;
declare function pidusage(pids: Array<number | string>): Promise<{ [key: string]: pidusage.Stat }>;
declare namespace pidusage {
interface Stat {
+17 -4
View File
@@ -18,8 +18,21 @@ pidusage(1, (err: Error | null, stats: pidusage.Stat) => {
timestamp = stats.timestamp;
});
let stats: Promise<pidusage.Stat>;
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.Stat> = pidusage(1);
const stats_two: Promise<pidusage.Stat> = pidusage('two');
const stats_obj: Promise<{ [key: string]: pidusage.Stat }> = pidusage([1, 'two']);