SoFunction
Updated on 2025-02-28

Summary of data type instances in Typescript

What is it

Typescript and JavaScript are almost the same, with the same data types. In addition, more practical types are provided for development and use based on JavaScript.

During the development stage, you can define a certain type for explicit variables, so that typescript can perform type checking during the compilation stage. When the type does not meet the expected results, an error message will appear.

What are there

The main data types of typescript are as follows:

  • 1. boolean (boolean type)
  • 2. number (number type)
  • 3. string (string type)
  • 4. Array (array type)
  • 5. Tuple (tuple type)
  • 6. Enum (enum type)
  • 7. Any (any type)
  • 8. Null and undefined types
  • 9. Void type
  • 10. Never type
  • 11. object (object type)

boolean

let flag:boolean = true;
flag = 123;    // mistakeflag = false;  // correct

number

Number types, like JavaScript, the numeric types of typescript are floating-point numbers, which can support binary, octal, decimal and hexadecimal.

let num:number = 123;
num = '456';   // mistakenum = 456;     // correct

Category representation:

let decLiteral:number = 6; // Decimallet hexLiteral:number = 0xfood; // hexadecimallet binaryLiteral:number = 0b1010; // Binarylet octalLiteral:number = 0o744; // Octal

string

String type, like javascript, can be used to represent strings using double quotes (") or single quotes (')

let str:string = 'this is ts';
str = 'test';

As a superset, of course, you can also use template strings to embed variables through ${}

let name:string = `Gene`;
let sentence:string = `Hello, my name is ${name}`

array

The array type is the same as JavaScript, and it is wrapped through []. There are two ways to write it:

Method 1: Follow the element type and []

let arr:string[] = ['12','23'];
arr = ['45','56'];

Method 2: Use array generics, Array<element type>:

let arr:Array<number> = [1,2];
arr = ['45','55'];

tuple

Tuple type, allowing an array of known elements and types, and the types of each element do not have to be the same.

let tupleArr:[number,string,boolean];
tupleArr = [12,'34',true]; // yes
tupleArr = [12,'34']; // no

enum

The enum type is a complement to the JavaScript standard data type. Using an enum type can give a set of numeric values ​​a friendly name.

enum Color {Red,Green,Blue}
let c:Color = ;

any

You can specify any type of value. It is not clear that the type variable is specified in the programming stage. You do not want the type checker to check these values ​​but directly let them pass the compilation stage. At this time, you can use any type

Use any type to allow assignment to any type, and even its properties and methods can be called

let num:any = 123;
num = 'str';
num = true;

When defining an array that stores various types of data, the sample code is as follows:

let arrayList:any[] = [1,false,'fine'];
arrayList[1] = 100;

null and undefined

In javascript, null means "nothing", is a special type with only one value, representing an empty object reference, and undefined means a variable with no value set.

By default, null and undefined are subtypes of all types, which means that you can assign null and undefined to variables of type number.

let num:number | undefined; // numerical type or undefined(num); // correctnum = 123; 
(num); // correct

However, ts is configured with the --strictNullChecks tag, null and undefined can only be assigned to void and their respective values

void

Used to identify the type of return value of the method, indicating that the method has no return value.

function hello():void {
    alert('Hello Runoob')
}

never

never is a subtype of other types (including null and undefined), which can be assigned to any type, representing values ​​that never appear.

But no type is a subtype of never, which means that the variables of life never can only be assigned by the never type.

The never type is generally used to specify which exceptions will always be thrown, infinite loops

let a:never;
a = 123; // Wrong writing
a = (() =&gt; { // Correct writing    throw new Errow('mistake')
})()

// The returned never function must have an unattainable endpointfunction error(message:string): never {
    thorw new Error(message);
}

object

Object type, non-primitive type, common form wrapped by {}

let obj:object;
obj = {name:'Zhang',age:23};

Summarize

It is basically the same as javascript and is also divided into:

  • 1. Basic Type
  • 2. Reference Type

In terms of basic types, typescript adds original types such as void, any, and emon.

This is all about this article about Typescript data types. For more related Typescript data types, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!