Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
Typescript JavaScript

How to use Typescript with arrays

  • June 28, 2021July 11, 2021
  • by Coder Kai

Array is used to store a sequence of elements as references. When declaring typescript array, the type of the elements should be defined in its syntax.

1. Initialize Array

Create Array with the type

const stringArray1:string[] = ['a', 'b'];
const stringArray2:Array = ['a', 'b'];

const numberArray1:number[] = [1, 2];
const numberArray2: Array = [1, 2];

Both of the above declarations are similar. One follows T<U> generic signature and other uses primitive types.

2. Declare and assign values

First, we are going to initialize the array and then assign the value. 

let array1: string[];
array1 = ['apple', 'banana'];

let array2:Array<string>;
array2 = ['apple', 'banana'];

Note that you cannot assign different values to the declared types. It will give a linting error from the type checker.

let errorArray:Array<string>;
errorArray = [1, '12']; // Type error

3. Use multiple data types in same array without order

You can use two or more types of data in a single array without a specific order as follows

let array1:Array = [1, '12'];
let array2:(string | number)[] = [2, 'foo'];

let array3:Array;
array3 = [1, '12'];

4. Create array with types or tuples

This is a way to create an array specific with types along with the position of the element

let array1:[number, string] = [1, '2'];
let array2:[number, string] = ['doo', 'foo']; // Error

One of the common example is React hooks you find in functional components

type SetStateType = [string, (param: string) => void]; 

const useState = (value:string): SetStateType => {
    let state = value; 
    return [state, (param: string) => {
      state = param; 
    }
  ]; 
};

let [value, setValue]: SetStateType = useState('string');
// this is not the exact implementation. Just a demonstration

5. Usage with generics

You can use an array with generics as in the following example.

interface Person {
    name: string;
    age: number;
}

let personArray1:Person[] = [{name: 'John', age: 12}];
let personArray2:Array = [{name: 'Doe', age: 22}];

6. Examples

Following are some examples with array prototype functions.

interface Person {
  name: string;
  age: number;
}

let people:Person[] = [{name: 'John', age: 12}];
people.push({name: 'Doe', age: 33});

people.splice(0, 1, {name: 'Foo', age: 15});
console.log(people[0].name); // "Foo"

const elderPeople:Array<Person> = people.filter(p => p.age > 13);

if (elderPeople.length > 0) {
  const elderNames: string[] = elderPeople.map(p => p.name);
  console.log('Elder guys: ', elderNames.join(', '));
  // "Elder guys: ", "Foo, Doe"
}

const elderCircle = elderPeople.concat({name: 'Pony', age: 1});
console.log(elderCircle[2].name); // "Pony"
  • Typescript Arrays usage

How to optimize react-native map views
How to add styles to stripe elements without using CardElement
Coder Kai
A humble developer
arrays javascript arrays Typescript typescript arrays typescript gude typescript tutorial

Related articles

Multiple refs for an array of React Elements
Using multiple refs for an…
Immutable and Mutable Values in Javascript
07. Immutable and Mutable Values…
wrapper objects in javascript
06. Wrapper objects in Javascript
globals undefined and null values in javascript
05 Global, null and undefined…
Javascript Booleans and Equality
04. Javascript Guide Booleans and…
How to add Chakra UI Animations
Chakra UI Animations
SSL Websocket using Nginx Proxy
SSL Websocket proxy with Nginx…
Change python version correctly
Python is not setting correct…
optimize React.js load time
How to optimize React.js app…
Multiple refs for an array of React Elements
How to use IntersectionObserver to…
Multiple refs for an array of React Elements
How to dismiss dropdowns when…
Javascript guide Strings
03. Javascript Guide – Strings
How to fetch data with useEffect
How to fetch data with…
add styles to stripe elements
How to add styles to…
how to optimize react-native map view
How to optimize react-native map…
debounce with react hooks
Avoid multiple clicks using debounce…
Numbers inJavascript
02. Javascript Guide – Numbers
Introduction to Javascript
01. Javascript Guide – Introduction…
Nginx Load Balancer with docker…
Create React App Using npx

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • Pamela587 on Chakra UI Animations
  • Alicia1760 on Chakra UI Animations
  • vneuweluso on How to add styles to stripe elements without using CardElement

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress