I am trying to use shelljs (via DefinitelyTyped) in Typescript 1.5-beta. In my code I would like to use the exec
function which has the following signature:
export function exec(command: string, options: ExecOptions): ExecOutputReturnValue | child.ChildProcess;
export interface ExecOutputReturnValue
{
code: number;
output: string;
}
If I import and use the library as follows (which, in normal ES6 JavaScript works perfectly fine)
import * as $ from 'shelljs';
const code = $.exec(command, { silent: true }).code;
the Typescript compiler gives me error TS2339: Property 'code' does not exist on type 'ChildProcess | ExecOutputReturnValue'
.
What can I do to access .code in a type-safe way?
In TypeScript, a union type variable is a variable which can store multiple type of values (i.e. number, string etc). A union type allows us to define a variable with multiple types. The union type variables are defined using the pipe ( '|' ) symbol between the types. The union types help in some special situations.
TypeScript 1.4 gives programs the ability to combine one or two types. Union types are a powerful way to express a value that can be one of the several types. Two or more data types are combined using the pipe symbol (|) to denote a Union Type.
To check if a string is in a union type: Create a reusable function that takes a string as a parameter. Add the values of the union type of an array. Use the includes() method to check if the string is contained in the array.
We can return any type of value from the function or nothing at all from the function in TypeScript. Some of the return types is a string, number, object or any, etc.
When you have a union type, you will see any shared members when you use it raw.
If you want to use more specific members, you need to use a type guard. Inside of the type guard, you will have access to all of the specific members for the type.
Here is a cut down example:
declare class Test {
example(): string | number;
}
var x = new Test();
var y = x.example();
if (typeof y === 'string') {
// In here, y has all the members of a string type
y.
} else {
// In here, y has all the members of a number type
y.
}
When you are dealing with types that you can't apply a typeof
check, you'll need to tell the compiler that "you know best":
const code = (<ExecOutputReturnValue >$.exec(command, { silent: true })).code;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With