【TS】Study notes

1. All JavaScript codes are valid Typescript codes. Use the TypeScript compiler to compile the Javascript code in TypeScript. The compiled result is exactly the same as the original Javascript code, that is, the file extension is changed from .js to .ts, which will not cause any negative impact

2. Migrate JavaScript code to TypeScript. TypeScript will perform type checking on the code and will receive some error messages. Using any can solve most of the error reporting problems

insert image description here
insert image description here
insert image description here
insert image description here
3. Type annotations Add
type annotations to function parameters, and add constraints to functions or variables. In the example, the greeter function is expected to receive a field string parameter. If the parameter is changed to an array, an error message will be generated after
insert image description here
recompilation .
insert image description here
insert image description here


insert image description here

insert image description here

insert image description here
4. Underfined and null
insert image description here
5. Array, there are two ways to define an array.
Connect [] after the original type
insert image description here
to use array generics, Array<element type>

insert image description here
The Tuple
tuple type allows to represent an array with a known number and type of elements, and the types of each element do not have to be the same

Enum enum
is a supplement to Javascript standard data types

Any
does not want the type checker to check the value but let them pass the check of the compilation stage directly, and mark these variables with any type
insert image description here

void
In a way, the void type is like the opposite of any type, it means that there is no type. When a function does not return a value, you usually see that its return type is void

object

object represents a non-primitive type, that is, a type other than number, string, and boolean.

Union type
Union types (Union Types) indicate that the value can be one of multiple types
insert image description here
Type assertion
No special data checking and deconstruction
Angle bracket syntax, as syntax
insert image description here
Type inference
Type inference: TS will infer a type when there is no clear specified type.
There are the following two situations: 1. When a variable is assigned a value, it is inferred to be the corresponding type. 2. When the variable is not assigned a value, it is inferred to be any type
insert image description here

Guess you like

Origin blog.csdn.net/weixin_49668076/article/details/131512119