TS Learning Notes 1

Definition: typescript is a superset of JavaScript and essentially adds optional static typing and class-based object-oriented programming to the language

The base type

There are 12 base types of TS in the typescript documentation

  • Boolean (Boolean)
  • Number (number)
  • String (string)
  • Array (Array)
  • The Tuple (a Tuple)
  • Enum (enumeration)
  • Any (any value)
  • Void (null)
  • undefined
  • null
  • Never (value that never exists)
  • The Object (objects)

When you declare a variable and assign a type, you get an error when you assign a variable to another type


let a: number = 11

a = '111' // Error: Cannot assign type "string" to type "number"

Copy the code

Of course, when you declare a definition, TS will automatically recognize the type of the value at the time of the definition, but it is important to follow the code specification and specify the variable type when you declare it


let a = 11

a = '111' // Error: Cannot assign type "string" to type "number"

Copy the code

Some of them are special:

The Array type can be declared in either of the following ways:

let arr = number[]  = [1.2.3] // The array item must be of type number

// Array generic declaration
let arr2 = Array<number> = [1.2.3]
Copy the code

The Tuple type allows you to represent an array with a known number and type of elements, not necessarily the same type

An array that holds each item type and number of items

    let tuple: [number.string]
    
    tuple = [1.2]  // Error: Cannot assign type "number" to type "string"
    tuple = [1.'q'.3] // Error: Cannot assign type "[number, string, number]" to "[number, string]". The source has three elements, but the target is only allowed two.
    // The number of items is correct and each item is of the correct type
    tuple = [1.'a']
Copy the code

Enum types complement the JavaScript standard data types. As in other languages such as C#, enumeration types can be used to give friendly names to a set of values

Feel like you haven’t used this type much

enum Color { Red, Green, Blue }

let c: Color = Color.Green
// By default, elements are numbered from 0. You can also manually specify the values of the members
enum Color { Red = 1, Green = 2, Blue = 4 }

let c: Color = Color.Green
let colorName: string = Color[2] // Green
Copy the code

Any type: Variable types are dynamic or if you want to be lazy and not strictly define a type for a variable, use it

Void: You know what this is if you’ve studied C. That’s basically what you do when a function doesn’t return a value, it’s not very useful to declare a void variable, because you can only give it undefined and null, right

function warnUser() :void {
    console.log("This is my warning message")}let unusable: void = undefined
Copy the code

Null and undefined are also useless. By default null and undefined are subtypes of all types. This means that you can assign null and undefined to variables of type number

The never type represents the types of values that never exist. The never type is the return type of function expressions or arrow function expressions that always throw an exception or have no return value at all

function error(message: string) :never {
    throw new Error(message)
}
Copy the code
assertions

Just tell TS what type it is

let someValue: any = "this is a string"
/ / method
let strLength: number = (<string>someValue).length
/ / method 2
let strLength: number = (someValue as string).length
Copy the code

*** Note that ⚠️ only as syntax assertions are allowed when JSX is used in TypeScript