Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a Typescript object return value for a function?

Tags:

typescript

My function looks like this:

parseRange = (text) => {     var arr = [];     var lower = null;     var upper = null;     if (!text || text === "") {         lower = null;         upper = null;     }     else if (text.indexOf("-") > 0) {         arr = text.split("-");         lower = +arr[0];         upper = +arr[1];     }     else {         lower = +text;         upper = null;     }     return {         lower: lower,         upper: upper     }; }; 

I am familiar with returning strings and numbers but how can I specify the return is an object with a lower and upper parameter?

like image 963
Alan2 Avatar asked Sep 08 '14 08:09

Alan2


People also ask

How do I return a value from a TypeScript function?

The function's return type is string. Line function returns a string value to the caller. This is achieved by the return statement. The function greet() returns a string, which is stored in the variable msg.

How do you pass an object as a parameter in TypeScript?

Inside the function we assign the parameters to properties in the object. To do this, we have to specify the this keyword, which refers to the calling object. The variables and parameters may have the same names. Anything we pass to the constructor as an argument, will be assigned to the property of the object.

Can a function return an object?

A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function.


2 Answers

parseRange = (text: string) : { lower: number; upper: number; } => {     // ...     return {         lower: lower,         upper: upper     }; }; 

or

parseRange = <(text: string) : { lower: number; upper: number; }> ((text) => {     // ...     return {         lower: lower,         upper: upper     }; }); 

or

var parseRange : (text: string) => { lower: number; upper: number; } = (text) => {     // ...     return {         lower: lower,         upper: upper     }; }; 

or

parseRange = function (text: string) : { lower: number; upper: number; } {     // ...     return {         lower: lower,         upper: upper     }; }; 

or

function parseRange(text: string) : { lower: number; upper: number; } {     // ...     return {         lower: lower,         upper: upper     }; }; 

or

interface RangeResult {     lower: number;     upper: number; } function parseRange(text: string) : RangeResult {     // ...     return {         lower: lower,         upper: upper     }; }; 
like image 118
Markus Jarderot Avatar answered Oct 07 '22 00:10

Markus Jarderot


TypeScript infers function return types, so this results in a compile error without having to specify the types explicitly:

var parseRange = (text) => {     return {         lower: 5,         upper: 6     }; };  var range = parseRange(""); range.foo; // compile error 

Live example which shows the error:

The property 'foo' does not exist on value of type '{ lower: number; upper: number; }'.

like image 37
Douglas Avatar answered Oct 07 '22 00:10

Douglas