This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Any type

Let’s look at an example:

let seven: number = 7;
seven = 'Seven';
Copy the code

If you run the code above, you get an error because TS does not allow you to assign type “Seven” to type “number”.

So what now?

We can change the number to any, right?

let seven: any = 7;
seven = 'Seven';
Copy the code

Of course, in addition to explicitly specifying any above, we can also define variables without assigning values, and then automatically infer any by type inference.

let seven;

seven = 7;

seven = 'Seven';
Copy the code

So what’s bad about the any type?

If it’s number and we use seven, the editor will give us a hint as to what the methods are and what they do. When we use a method, the editor will also indicate what the method’s parameters are.

After changing to any, there will be no prompt.

let seven: number = 7;

seven.toFixed(3);
Copy the code

So the any type cannot be abused, so when do we use any?

  • When the third-party library does not provide a type file
  • When type conversion is difficult
  • Data structures (JSON) are too complex

Basic packaging object

concept

JavaScript comes in two types: Primitive data types and Object types.

Primitive data types include Boolean, numeric, string, null, undefined, and the new type Symbol in ES6. Remember that all primitive data types do not have properties.

As mentioned above, strings are treated as primitive data types rather than object types in JavaScript, but let’s look at the following code.

let text='hello';

console.log(text.toUpperCase()); // HELLO
Copy the code

Text has a toUpperCase property. So is that not true? If text is not an object type, why does it have toUpperCase, toLowerCase, etc.?

A simple conclusion to start with: JavaScript makes a quick mandatory switch between the primitive data type and the object type.

A detailed explanation of the above code is as follows: When we try to access the toUpperCase property of a text, JavaScript forces the String value to be converted to an object type via new String(text), which is the Wrapper object.

It inherits all the methods of a string and is used to get references on its properties. Once the property is invoked, the wrapper object is discarded.

So the above code can be interpreted as actually doing the following:

let text = (new String('hello')).toUpperCase();
Copy the code

Wrapper object concerns for type declarations in TypeScript

We’ve already seen that TypeScript has a powerful type system, but we need to be aware of the following when assigning values.

let isDoing: boolean = true; // The compiler passes

let isTesting: boolean = Boolean(1) // The compiler passes

let isDone: boolean = new Boolean(1); // Failed to compile
Copy the code

The third line of code compiles in error. Why? The value generated by New Boolean() is actually a wrapped object, not a primitive data type. Here we expect isDone to be a primitive data type, so TypeScript will fail to compile.

The assignment of values and strings is similar and will not be repeated.