Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

At the same time, I participated in the Digitalstar project to win the creative gift package and challenge the creative incentive money

preface

Preliminary review

Getting started with TypeScript development environment and preparation

Basic knowledge of

The type hierarchy in TS

To highlight

  • A value of a lower type can be assigned to a variable/constant of an upper type

  • Variables/constants of type unknown can point to any type of value.

  • There is no variable of type never (never is an empty set)

Top/Bottom Type

contrast: Top and Bottom types in different languages

any

1. Any is special. It is both Top Type and Bottom Type

  • That is, variables/constants of type any and variables/constants of any other type can be assigned to each other

  • But any is type-insecure and unserviceable, so it should be avoided as much as possible

2. Any is contagious

  • It makes everything it touches Type Unsafe, so TS introduced the type-safe unknown Type as Top Type in 3.0

3. Any hides bugs

  • Because there is no type information, there is no error if it is used incorrectly.

4. Any hides code design details

  • Lost design of data types

Enable strict mode or disable implicit any in tsconfig To highlight

  • Turn noImplicityAny on. Avoid using any

unknown

To highlight

  • If the type cannot be predicted, do not use any, use unknown and narrow the type before using

Boolean type (Boolean)

Boolean types have only two elements: true and false

The number type

Number includes: integer, floating point, +Infinity, NaN

Bigint type

  • Bigint is a new type that can represent an integer of any size. The number range [- (2^53-1), 2^53-1]

  • Bigint literals are numbers followed by a lowercase “n”. Bigint supports addition, subtraction, multiplication, division, remainder %, and power **

String type (string)

Symbol type

  • Symbol is ES2015 | into the new language features

contrastString literal typeVS unique symbol

Object Type (Object)

To understand the following object types, consider the following two points

  • Defining objects and defining object types;

  • Get object keys and get object type keys

Array type (Array)

Arrays are annotated in two ways:

  • T[]

  • Array

    Interface generic

Tuples (a Tuple)

Tuples are subtypes of arrays. The element type at each index bit of a tuple is determined.

knowledge: Because tuples are created in the same way as arrays, tuples must beDisplay annotations.

[…string[]] is equivalent to string[], but [string,…string[]] is not equivalent to string[], the former contains at least one element

Enumeration (Enum)

Enumeration is essentially a mapping. An object containing the mapping is generated in the value space. Merging of enumerationsEnumerations can be split into multiple segments and can be merged with a namespace of the same name

Constant enumeration

  • Constant enumerations do not create variables in value space.

  • All references to constant enumerations are replaced with corresponding values

null,undefined,void,never

conclusion

If there is any mistake above, please point out in the comment section, thank you!

After reading it, give it a thumbs-up and then walk away