Typings a out of date
Closed this issue · 5 comments
The types here:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node-powershell/index.d.ts
are quite incomplete/out of date.
Can these be updated so we can use TypeScript?
For what it's worth, these typings should suffice. I'll contribute them to DT later.
Importing in TS with esModuleInterop
:
import shell, { ShellParameter } from 'node-powershell'; // You can also import the interfaces
declare module 'node-powershell' {
export interface ShellOptions {
/**
* Determines whether to log verbose to the console.
*/
debugMsg?: boolean;
/**
* Sets the input encoding for the current shell.
* @default 'utf8'
*/
inputEncoding?: string;
/**
* Sets the output encoding for the current shell.
* @default 'utf8'
*/
outputEncoding?: string;
/**
* Sets the default execution policy for the current shell session
*/
executionPolicy?: 'Bypass';
/**
* Determines whether to load the Windows PS profile
*/
noProfile?: boolean;
/**
* Instructs the Shell the use pwsh as the PowerShell runspace
* @default false
*/
pwsh?: boolean;
/**
* Instruct the Shell to use pwsh-preview as the PowerShell runspace.
* @default true
*/
pwshPrev?: boolean;
/**
* Determines whether to log verbose to the console.
* @default true
*/
verbose?: boolean;
}
export interface ShellStream {
stdin: NodeJS.WritableStream;
stdout: NodeJS.ReadableStream;
err: NodeJS.ReadableStream;
}
/**
* Structure for a single Shell parameter
*/
export interface ShellParameter {
/**
* The name of the parameter
*/
name?: string;
/**
* The value of the parameter
* @remark for switches this should be `''` (empty string), or `undefined`
*/
value?: string | number | boolean | unknown[] | Record<PropertyKey, unknown> | Date | undefined;
/**
* The name of the parameter with the value as its direct value
* @remark for switches this should be `''` (empty string), or `undefined`
*/
[key: string]: string | number | boolean | unknown[] | Record<PropertyKey, unknown> | Date | undefined;
}
class NodePowershell {
/**
* An array containing the command history ever added to the shell instance.
*/
public history: string[];
/**
* An object containing the {@link https://nodejs.org/api/child_process.html#child_process_child_stderr sdtio (in, out, err)} {@link https://nodejs.org/api/stream.html#stream_class_stream_readable [stream.Readable]} of the PowerShell Instance.
*/
public streams: ShellStream;
/**
* A number representing the process id the PowerShell instance got.
*/
public pid: number;
/**
* An array containing the commands that currently in the pipeline (before invoke() called).
*/
public commands: string[];
/**
* An array containing the commands ever invoked in the shell, and their results.
*/
public history: unknown[];
/**
* A string representing the execution state of the current PowerShell instance
*
* Read more: {@link https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.psinvocationstate?view=powershellsdk-1.1.0}
*/
public invocationStateInfo: string;
/**
* A boolean determines whether to log verbose to the console.
*/
public verbose: boolean;
/**
* Creates a new Shell instance. Starts by default, a powershell process on Windows, and a powershell-core (pwsh) process on Linux and macOS.
*/
public constructor(options: ShellOptions);
/**
* Adds a command to the end of the pipeline of the shell object.
* @param command A PowerShell command or a path to a PowerShell script.
* @returns A promise that fulfills with the array of commands currently in the pipeline, or rejects with an error.
*/
public addCommand(command: string): Promise<string[]>;
/**
* Listens to events emitted from the shell.
* @param eventName required - Possible events:
* - `output` - Emits when shell has an output.
* - `err` - Emits when shell has an error.
* - `end` - Emits when shell ends.
* @param callback required - Callback function to be called when the event emits.
*/
public on(eventName: 'output' | 'err' | 'end', callback: (output: string) => void): void;
/**
* Adds an argument to the last added command.
* @param argument The argument to add to the last command
* @returns A promise that fulfills with the array of commands currently in the pipeline, or rejects with an error.
*/
public addArgument(argument: string): Promise<string[]>;
/**
* Adds a parameter to the last added command.
* @param parameter The parameter to add to the last command.
* @returns A promise that fulfills with the array of commands currently in the pipeline, or rejects with an error.
*/
public addParameter(parameter: ShellParameter): Promise<string[]>;
/**
* Adds multiple parameters to the last added command.
* @param parameters The parameters array to add to the last command
* @returns A promise that fulfills with the array of commands currently in the pipeline, or rejects with an error.
*/
public addParameters(parameters: ShellParameter[]): Promise<string[]>;
/**
* Empty the commands array.
* @returns A promise that fulfills with an empty array of commands.
*/
public clear(): Promise<unknown[]>;
/**
* Runs the commands currently in the shell object pipeline.
* @returns A promise that fulfills with the output of all the commands that were in the pipeline before the call to this function, or rejects with an error.
*/
public invoke(): Promise<string>;
/**
* Releases all resources used by the shell object and closes the PowerShell `child_process`.
* @returns A promise that fulfills with the exit code of the child_process, or rejects with an error.
*/
public dispose(): Promise<string>;
}
export = NodePowershell;
}
I think I'm doing something wrong. So we installed the typings:
yarn add -D @types/node-powershell
Then when copy pasting the example in a TypeScript file:
import Shell from 'node-powershell'
const ps = new Shell({
executionPolicy: 'Bypass',
noProfile: true,
})
ps.addCommand('echo node-powershell')
ps.invoke()
.then((output) => { console.log(output) })
.catch((err) => { console.log(err) })
We get this error from TS:
What are we missing here?
@DarkLite1 I haven't gotten around PR'ing an update to @types/node-powershell. If you copy the types I commented above and dump them in your own .d.ts
file that's included in your tsconfig then it'll work. I'll work on making the PR to DefinitelyTyped now.
@DarkLite1 I've put up a PR at DT: DefinitelyTyped/DefinitelyTyped#52141
Lol'd, I forgot to post it here, but the aforementioned PR is merged and @types/node-powershell has been updated.
This issue can be closed @TylerLeonhardt
CC @DarkLite1