[internship diary] on the fifth day, analyze the source code + learn the basic syntax of Node.js & Typescript

The National Day holiday is over. In fact, internship here is quite relaxed and pleasant for me. Although there are many problems in the process of completing the task, it is also wrapped in the sense of achievement to overcome the problems. I feel like studying in the office every day. Come on, Xiao He!

After running example successfully, the next task is clearer. The first thing to complete is to learn the language node.js & typescript. I decided to start from scratch. This language is a C-like language, so there will be many similarities in syntax. So cheer yourself up! Sure! Come on, come on~

First of all, I read the declaration file of Typescript and learned the representation methods of some basic statements (but I haven't learned the logic yet). The links are as follows:

1, Know Typescript
2, Typescript Basics
3, Typescript advanced types and techniques
4, Best practices for TypeScript Engineering
(it's really awesome! You must see it!!!)

The main grammar learned are:
1, Raw data type

  • Boolean value
    Is the most basic data type. In TypeScript, boolean is used to define boolean value types:
let isDone: boolean = false;

// Compile passed
// As agreed later, code fragments that do not emphasize compilation errors are compiled by default
  • numerical value
    Use number to define the value type:
let decLiteral: number = 6;
let hexLiteral: number = 0xf00d;
// Binary representation in ES6
let binaryLiteral: number = 0b1010;
// Octal notation in ES6
let octalLiteral: number = 0o744;
let notANumber: number = NaN;
let infinityNumber: number = Infinity;
  • character string
    Use string to define string type:
let myName: string = 'Tom';
let myAge: number = 25;

// Template string
let sentence: string = `Hello, my name is ${myName}.
I'll be ${myAge + 1} years old next month.`;
  • Null value

JavaScript does not have the concept of void. In TypeScript, void can be used to represent functions without any return value:

function alertName(): void {
    alert('My name is Tom');

It's no use declaring a variable of void type, because you can only assign it to undefined and null.
In TypeScript, you can use null and undefined to define these two raw data types:

let u: undefined = undefined;
let n: null = null;

The difference from void is that undefined and null are subtypes of all types. That is, variables of type undefined can be assigned to variables of type number:

// This will not report an error
let num: number = undefined;
// This will not report an error
let u: undefined;
let num: number = u;

A variable of void type cannot be assigned to a variable of number type:

let u: void;
let num: number = u;

// Type 'void' is not assignable to type 'number'.

2, Arbitrary value
Any value is used to indicate that assignment to any type is allowed.
If it is a normal type, it is not allowed to change the type during assignment, but if it is any type, it is allowed to be assigned to any type.

let myFavoriteNumber: any = 'seven';
myFavoriteNumber = 7;

The above code is assigned repeatedly. The first time is string type and the second time is number type.

3, Type inference
TypeScript will infer a type when there is no explicit specified type, which is type inference. If there is no assignment at the time of definition, no matter whether there is assignment later, it will be inferred as any type and will not be checked at all:

let myFavoriteNumber;
myFavoriteNumber = 'seven';
myFavoriteNumber = 7;

4, Union type
Union Types means that the value can be one of a variety of types.

let myFavoriteNumber: string | number;
myFavoriteNumber = 'seven';
myFavoriteNumber = 7;

Union types use | to separate each type.

let myFavoriteNumber: string | number here means that the type of myFavoriteNumber is allowed to be string or number, but it cannot be other types.

let myFavoriteNumber: string | number;
myFavoriteNumber = true;

// index.ts(2,1): error TS2322: Type 'boolean' is not assignable to type 'string | number'.
//   Type 'boolean' is not assignable to type 'number'.

5, Object type - Interface
In TypeScript, we use Interfaces to define the type of object.
What is an interface?
In object-oriented language, Interfaces is a very important concept. It is the abstraction of behavior, and how to act needs to be implement ed by classes.

The interface in TypeScript is a very flexible concept. In addition to abstracting part of the behavior of a class, it is also commonly used to describe the "Shape of an object".

interface Person {
    name: string;
    age: number;

let tom: Person = {
    name: 'Tom',
    age: 25

In the above example, we defined an interface Person, and then defined a variable tom, whose type is Person. In this way, we constrain that the shape of tom must be consistent with the interface Person.

Interfaces are generally capitalized. In some programming languages, it is recommended to prefix the name of the interface with I.

It is not allowed to define more variables and fewer attributes than the interface:

interface Person {
    name: string;
    age: number;

let tom: Person = {
    name: 'Tom'

// index.ts(6,5): error TS2322: Type '{ name: string; }' is not assignable to type 'Person'.
//   Property 'age' is missing in type '{ name: string; }'.

Interface details (this chapter is really super awesome!! you must read it carefully!)

It's over. The more you read it, the more you don't understand it:
I took you to appreciate the computer tongue twister. I read it several times before I understood it:

  1. Once any attribute is defined, the types of the determined attribute and the optional attribute must be a subset of its types:
  2. The read-only constraint exists when the object is assigned for the first time, not when the read-only attribute is assigned for the first time:
  3. When the function expression | interface is used to define the function, type restrictions are carried out on the left side of the peer sign to ensure that the number of parameters, parameter types and return value types remain unchanged when assigning the function name in the future.

Tonight, I've finished reading all the basic node.js & typescript syntax, but I mainly understand some basic vocabulary and vocabulary usage and limitations, but I haven't seen the logic of node.js & typescript, so tomorrow's arrangement is to look at node.js & typescript syntax and logic.

Now sort out some useful sentences in blog cases:

  1. let (variable): (type) = (assigned value);
let decLiteral: number = 6;

Define a variable named decLiteral, type number, with a value of 6

let myName: string = 'Tom';

Define a variable named myName, string type and Tom value

let myFavoriteNumber: any = 'seven';

Define a variable named myFavoriteNumber, any (arbitrary) type and value seven

  1. var defines global variables

  2. let (variable): (type I) | (type II);

let myFavoriteNumber: string | number;

Define a variable named myFavoriteNumber of type string or number

  1. Interface interface name {name 1: type 1; name 2: type 2;}
interface Person {
    name: string;
    age: number;

Define an interface named Person. There are variables named name with type string and variables named age with type number in the interface.

Tags: Javascript node.js TypeScript

Posted on Fri, 08 Oct 2021 05:53:22 -0400 by BraniffNET