There are 7 basic types in TypeScript.
1. boolean
2, number
represents a number in JavaScript. In JavaScript, both "integers" and "floating-point numbers" are stored as double-precision floating-point types.
3. string
represents a string. Like JavaScript, you can use a pair of double quotes (") or a pair of single quotes (') to represent a string.
4. array
There are two methods of array declaration in TypeScript.
①Use "[]" to declare:
② Use array type to declare:
Both declaration methods can be used, and there will be no difference in the effect. However, it is recommended that only one of them should be used in the code to maintain a unified code style.
5. enum
The enumeration type is newly added in TypeScript, but there is no such type in JavaScript.
Same as C#, if the value of the first item is not declared, then the value of Red above is 0, and then each item is increased by one, that is, Green is 1 and Blue is 2.
So at this time the value of Red is 1, Green is 2, and Blue is 3.
Of course, you can also specify a value for each item.
In addition, enumeration types have a special function. If we have a value, but we don’t know whether it is defined in the enumeration type, we can get it in the following way:
Then Green and undefined will be output. Because Green has a value of 2 and no enum defines a value of 4, undefined is returned.
6. any
Like the default type of variables in JavaScript, references are dynamic and can be assigned any type. For example:
After defined as any, the syntax awareness function will be lost, which is equivalent to writing JavaScript.
It is worth mentioning that any can be used with arrays:
7, void
This type can only be used in functions. You can specify the return type of the function as void, which means that the function does not return any value.
The above is the entire content of this article, I hope you all like it.