If you are looking for a way to declare a function with an Array return type in TypeScript, this article is just what you need. You need to set the function’s return type for a function to return a value of a specified type. Check out the information below for detailed instructions.
Method to declare a function with an Array return type in TypeScript
For declaring a function with an Array return type in TypeScript, you need to specify the function’s return type to an array right after declaring this list of parameters. Take a look at the following examples to understand how it works.
// The function has a return type of strings array function getDaysInWeek(): string[] { return [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]; } const result = getDaysInWeek(); console.log(result); console.log(result instanceof Array);
Output:
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
true
In this example, we create the getDaysInWeek()function that takes no input parameters and returns an array with each element of type ‘string’.
The ‘result’ variable stores the return value of the getDaysInWeek()function.
You can see that the ‘result’ is actually an array when you print it to the screen. Moreover, using the instanceof method to verify, we also get the ‘true’ value.
Besides, you can also specify the function’s return type as an array that has elements of different types by using a union.
// Using a union (|) function daysInWeek(): (number | string)[] { return [2, 3, 4, 5, "Friday", "Saturday", "Sunday"]; } const result = daysInWeek(); console.log(result); console.log(result instanceof Array);
Output:
[2, 3, 4, 5, "Friday", "Saturday", "Sunday"]
true
At this point, the getDaysInWeek()function returns an array of numbers and strings.
In case you want the function to return an array of objects, you need to create a new type/interface. Then set the return type of the function as the type/interface you defined. Here is an example for you:
// Define a new type type StudentScore = { mathScore: number; physicsScore: number; chemistryScore: number; }; // The function has a return type of StudentScore[] function getScore( mathScore: number, physicsScore: number, chemistryScore: number ): StudentScore[] { return [{ mathScore, physicsScore, chemistryScore }]; } const result = getScore(9, 9.5, 8.5); console.log(result);
Output:
[{
"mathScore": 9,
"physicsScore": 9.5,
"chemistryScore": 8.5
}]
We define a new type named ‘StudentScore’ with three properties: mathScore, physicsScore and chemistryScore, then type it as the function’s return type.
The getDaysInWeek()function will now return an object array of the above properties respectively.
Summary
Finally, to declare a function with an Array return type in TypeScript, you can set the function’s return type to an array right after declaring this list of parameters. That’s all we want to convey through this article. Hopefully, the information we provide in this tutorial will be helpful to you.

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js