This is the third day of my participation in the August More Text Challenge.

type

Basic types of

Typescript supports restrictions on the types of variable stores based on JavaScript syntax

Defining base types

  • String and number and Boolean and symbol *String Number Boolean SymbolIs the interface defined by String, Number, Boolean, Symbol
let str: string = 'String'
let num: number = 12
let bool: boolean = false
let sbl: symbol = Symbol(a)Copy the code
  • Null, and undefined

    *By default, null and undefined are subtypes of all types. All types are duly typed and can also be assigned null and undefined.
let u: undefined = undefined
let n: null = null
Copy the code

The complex type

  • An Array of Array

    *Unlike js, arrays in TS can only hold certain types of data inside them.

    *You can useanyStore any type.

    *You can use|Specify multiple types.
let list: number[] = [1.2.3]
let list: Array<number> = [1.2.3]; // Interface paradigm
let list: (number|string) [] = [1.2.3.'123']
letList: [] = [1.2.'3'] // Error, this is a tuple
Copy the code
  • The Object and the Object

    *Object represents a non-primitive type, that is, a type other than number, string, Boolean, symbol, NULL, or undefined.

    *Object can be a type defined by the Object interface, and object can be any value.

    *Object is the interface that defines Object objects. Only methods contained in the Object interface can be called.
let obj: object = []
let obj: object = {}
Copy the code

other

  • tuples

    *An array representing a fixed element.
let tuple: [number.number|string] = [1.2]
tuple = [1.'2']
Copy the code
  • The enumeration

    *Enumeration is essentially a key-value object, starting at 0 by default, and also serving as a key, whose value is the corresponding key.

    *Not an array,em[2]This is actually access2.This property, it’s not a subscript.

    *You don’t need to know what the value is, but you can set some or all of it.
enum type = {right, center, left = 5}
type.right / / 0
type[2]    // undefined
type[5]    // undefined
Copy the code
  • Any

    *anyThe Object interface can be used to call any method.
let notSure: any = 4 
notSure.ifItExists()  / / no checks
notSure.toFixed()     // check that there must be number.prototype.tofixed

let prettySure: Object = 4  // Object values can be of any type
prettySure.toFixed()        // Check the properties of the Object interface
Copy the code
  • Unknown [3.0 +]

    *unknown Indicates an unknown type, which is type checked and can only be assigned toanyunknown. You can’t do anything else on the unknown type without shrinking it.
let un: unknown = 4 
let ay: any = un
let Obj: Object = un
let num: number = un // Type "unknown" cannot be assigned to type "number".
let obj: object = un // Type "unknown" cannot be assigned to type "object".
Copy the code

Type narrowing can be done using type assertion and type narrowing

let un: unknown = 4
let num: number = <number>un // Type assertion
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
let un: unknown = 4
let num: number
typeof un === 'number' && (num = un) // Type contraction we handle the type judgment ourselves
Copy the code
  • The void in void *js is a unary operator that can be followed by any value or expression. Returns undefined, with a higher priority. * ts, the value is undefined or null, void said equivalent type is set to undefinde | null.

  • Never

    The never type represents the types of values that never exist. The never type is a subtype of any type and can be assigned to any type; However, no type is a subtype of never or can be assigned to a type of never (except never itself). Even any cannot be assigned to never.

    A function that does not return a value. By default, this function returns undefined. Undefined does not return.

// A function that returns never must have an unreachable end
function error(message: string) :never {
    throw new Error(message)
}

// The inferred return value type is never // error is never
function fail() {
    return error("Something failed")}// A function that returns never must have an unreachable end
function infiniteLoop() :never {
    while (true) {}}Copy the code
  • Types of assertions

    *Type of temporary change when in use.

    *When JSX is used in TypeScript, only AS syntax assertions are allowed.
// Angle bracket syntax
let someValue: any = "this is a string"
let strLength: number = (<string>someValue).length

let someValue: number = 1223;
let strLength: number = (<Object>someValue) Obeject cannot be assigned to number

/ / as syntax
let someValue: any = "this is a string"
let strLength: number = (someValue as string).length   
Copy the code
  • Deconstruction statement

    *Define the object property a: string b: string on the right and name a newa when destructing assignment
let {a: newa, b}: {a: string.b:string} = {a: 'null'.b: null}
newa // 'null'
b    // null
Copy the code
  • optional
let obj: {a: any, b? :number}
obj = {a: 1234123} // b can exist or not exist
Copy the code

* To declare attributes for an object, you need to specify all owned and possible owned attributes. If you are unsure, you can add any attribute type

  • Object arbitrary property and read-only type
interface J {
    readonly id: number
    [propName: string] :any  // This is set to any type
  }
  let obj1: J = {
      id: 1.name: 'name'.age: 18
  }
  obj1.id = 2 // The id is read-only
Copy the code

* What if the object key is a number? You can use mapping object types

  • Mapping object type
type J = {
    [propName in string | number] :any // This is set to any type; } and {readonly id: number;
};

let obj: J = {
    id: 1.name: 'haha'.age: 4.a: 1.b: 2.1: 5
}
Copy the code

* What if the object key is of type symbol?

  • Object key symbol type /unique symbol type
const key1 = Symbol(a)const key2: symbol = Symbol(a)const key3: unique symbol = Symbol(a)2.7 + / / TS
type O = {
    [propName in string | number] :any} and {readonly id: number; [key1]? :any, [key2]? :any.// The computed attribute name in the type text must reference an expression of type text or "unique symbol".[key3]? :any,}let obj1: O = {
    id: 1.name: 'haha'.age: 4.a: 1.1: 5
}
obj1[key1] = 2
obj1[key2] = 2 // Type "symbol" cannot be used as an index type.
obj1[key3] = 2
Copy the code