Current location - Quotes Website - Signature design - Ts self-study notes
Ts self-study notes
is the type of cast, and artificial type cast

is the first type cast using angle brackets (<; string> SomeValue).length

The second one uses the as keyword. Length

TS uses let and const to declare variables, and it has a block-level scope (curly bracket scope)

Use the principle of least privilege. All variables except those you plan to modify should be deconstructed with const

const

const {a, b} = {a: first ,second] = [1, b: 1,2}

You can add the default value {a,b=first ,second] = [1}

... The operator has the function of expansion

let first =

let a. It can be called cramming or structural subtype.

an interface can declare optional attributes, and the implementation does not necessarily need to have such attributes

interface ISquare{

color? :string;

width? :number;

}

Some attributes can only be assigned when the object is instantiated

Interface point {

Readonly x: number;

readonly y:number;

}

let p:point = {x:first ,second] = [1,y:first ,second] = [1}

pfirst ,second] = [1.x=5 //error

ReadonlyArray< T> Type can create a read-only array

let r: readonlyarray <; number> =

r [] = 1,2//error

readonly vs const

If the object is restricted, const is used; if the object is restricted, readonly is used

In general, the implementation of the interface needs to meet all the attributes just covered. However, sometimes there is a need to dynamically add additional attributes

Just like languages like java

extends keywords: type java

refers to the parent class through the super keyword (this refers to this class). This example also demonstrates how to override the method of the parent class in subclasses

Members whose private modifiers can only be accessed in this class

public * * * Modifiers can be accessed everywhere

protected modifiers can be accessed in inherited classes

readonly decorated read-only properties must be initialized in declarations or constructors

Generally used as the base class of derived classes and cannot be instantiated. The difference between an abstract class and an interface is that the abstract class has more details of its members.

The abstract keyword is used to define the abstract class and the line drawing method.

Subclasses inherit the abstract class and can rewrite the methods inside.

Define a function completely.

Function assignment is inferred according to the type without specifying a specific type.

Unknown parameters are merged into a collection.

js language, This is specified when the function is called, and this inside the function points to the object calling the function. If there is no object, it points to window. In strict mode, it is undefined

, that is, the function signature is the same, but the parameter types, number and return value types are different;

js is a dynamic type with variable parameters, so there is no such thing as overloading.

But TS can define the overloading of methods just like c#.

When creating a function or an object, the types of parameters are uncertain, or when it can be applied to multiple types, it can be defined by generics.

Unlike any, any does not restrict the types of incoming parameters at all. However, generics will constrain the type consistency of incoming parameters and return values

If generics are not constrained, all types need to be verified by operations. For example, string has the length attribute but boolean does not. If we use the length attribute directly on generics, we will report an error. Assuming that we use generic constraints to constrain that types must have the length attribute (that is, we must inherit the interface containing the length attribute), there will be no error

We hope that a variable can support multiple types at the same time, and use vertical lines to separate

this type represents a class or the interface itself.