Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use union type return value in Typescript?

Tags:

typescript

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?

like image 651
left4bread Avatar asked Jul 15 '15 17:07

left4bread


People also ask

How do you use a union in TypeScript?

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.

How do you declare a union type variable in TypeScript?

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.

How do you determine the value of 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.

Can a function return a type TypeScript?

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.


1 Answers

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;
like image 131
Fenton Avatar answered Oct 10 '22 13:10

Fenton