jamesshore / test-console

A simple and pragmatic library for testing Node.js console output.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Typescript types

0vidiu opened this issue · comments

Hello,

Since I found your module helpful I'd like to contribute with the Typescript type declarations. This is my first go at this so, not entirely sure it's 100% correct, but it seems to work fine for me. I wrote this so Typescript compiler would stops bugging me. Maybe it will be helpful to others, as well.

declare module 'test-console' {
  type Callback = (output: string) => void;
  type Output = string[];
  type Options = {
    [key: string]: any;
    isTTY?: boolean;
  };
  type Inspector = {
    output: Output;
    restore(): void;
  };

  export const stdout: {
    inspect(options?: Options): Inspector,
    inspectSync(fn: Callback);
    inspectSync(options: Options, fn?: Callback): Output,
    ignore(options?: Options): void,
    ignoreSync(fn: Callback): void,
    ignoreSync(options: Options, fn?: Callback): void,
  };

  export const stderr: {
    inspect(options?: Options): Inspector,
    inspectSync(fn: Callback): Output,
    inspectSync(options: Options, fn?: Callback): Output,
    ignore(options?: Options): void,
    ignoreSync(fn: Callback): string,
    ignoreSync(options: Options, fn?: Callback): string,
  };
}

Thanks!

Thanks for the submission! I don't use TypeScript, so I can't validate this—and I'm uncomfortable merging code I can't check or maintain. But I'm happy to leave this pull request open for anybody who wants to use it.

@jamesshore Sure no problem! It's understandable. :)

Hi @jamesshore @0vidiu!

@jamesshore we could add the types for test-console to DefinitelyTyped, this way you don't need to worry about maintain them.

@0vidiu I've done a few changes:

declare module 'test-console' {
  type Output = string[];
  type OutputCallback = (output: Output) => void;
  type NoOutputCallback = () => void; // ignore* functions doesn't pass output to the callback
  type Restore = () => void; // used by Inspector and function ignore, not sure about this
  type Options = {
    [key: string]: any;
    isTTY?: boolean;
  };
  type Inspector = {
    output: Output;
    restore: Restore;
  };

  type Std = { // same type for stdout and stderr
    inspect(options?: Options): Inspector,
    inspectSync(fn: OutputCallback): Output;
    inspectSync(options: Options, fn?: OutputCallback): Output,
    ignore(options?: Options): Restore,
    ignoreSync(fn: NoOutputCallback): void,
    ignoreSync(options: Options, fn?: NoOutputCallback): void,
  }

  export const stdout: Std;
  export const stderr: Std;
}

What do you think?
I can submit to DefinitelyTyped, If it's not a problem for you.

I can submit to DefinitelyTyped, If it's not a problem for you.

Sure, fine by me. I don't use TypeScript, so I can't verify the code you posted.