“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

preface

We know that TS is a superset of JS, which will eventually compile into JS. TS is much more than JS. Today we’ll look at the data types that correspond to JS in TS

For convenience, the next part is planned to cover what types TS has more than JS

This article first introduces the types that both JS and TS have

JS and TS

Take a look at the following eight data types:

  1. Basic data types:number.string.boolean.undefind.null.symbol.bigInt

Among them, the last two are added later (Symbol is added to ES2015, bigInt is added to ES2020).

  1. Complex data types:objecttype

Each of these types has a corresponding type in TS, so let’s look at them one by one

The number type

In TS, you can use number to represent numeric types

let count: number = 0
Copy the code

Note: As with JS, numbers can be represented in binary, octal, decimal and hexadecimal as well as NaN and Infinity

Type string

In TS, string is used to represent character types

let name: string = 'LBJ' 
Copy the code

Boolean type

In TS, Boolean is used to represent Boolean types

const isTrue: boolean = true
Copy the code

Note that Boolean types in TS can only be true or false

Null and undefined

let n1: null = null
let u1: undefined = undefined
Copy the code

Note: If defined as null or undefined, the assignment can be either null or undefined

That means the following is also ok

let n2: null = undefined
let u2: undefined = null
Copy the code

Symbol type

const symbol1: Symbol = Symbol('Related Description')
Copy the code

BigInt type

// bigint A numeric value can be followed by the letter n
let b1: bigint = 999999999999999999n
// You can also use the BigInt constructor
const b2: bigint = BigInt('9999999999999')

Copy the code

The object type

Actually in ts have object types, different is its object and {} type, will differ between them, a simple memory is as follows:

  • objectTypes are used to represent non-primitive types
  • ObjectThe type is allObjectThe type of an instance of the class
  • {}A type describes an object with no members

conclusion

That’s all for this article, which covers 8 types that are common to JS and TS. In the next part, we’ll look at data types that are unique to TS

If you have any questions, please point out, thank you!