Foreword: no matter what language we are learning, we need to understand how to define a variable or Function. Today, let's take a look at the data types of TypeScript.
1, Difference between Ts and Js
As we all know, JavaScript is a scripting language, and its official name is ECMAScript (because the standard for defining the language is ECMA-262).
Main features of JS:
1. The syntax is similar to common high-level languages, such as C and Java;
2. Script language, which can be run directly by the interpreter without compilation;
3. Variables are loosely defined and belong to weakly typed languages;
4. Object oriented
TypeScript is developed under the leadership of Anders Hejlsberg, the great God of Microsoft (Anders heilsberg, Danish, main author of Turbo Pascal compiler, leader in Delphi and C# development, and one of the founders of. NET). It is compatible with ECMAScript 2015 (ES6) specification, and can optionally be compiled into JavaScript code of ES6 or ES5 specification (ECMAScript 3 and above).
TypeScript is a language with the following main features:
- Object oriented and has some functional features;
- Type language;
- It realizes the characteristics of annotation, genericity and so on;
- Adapt to large App construction.
2, Data type of Ts
There are about 7 data types of Ts, namely:
data type | keyword | describe |
---|---|---|
Any type | any | Variables declared as any can be given any type of value |
Number type | number | Double precision 64 bit floating point value. It can be used to represent integers and fractions (which can represent hexadecimal data) |
String type | string | A character series that uses single quotation marks (') or double quotation marks (") to represent a string type. Back quotation marks (`) define multiline text and embedded expressions |
Boolean type | boolean | Indicates logical values: true and false |
Array type | nothing | Declare variables as arrays |
tuple | nothing | Tuple type is used to represent an array of known element numbers and types. The types of each element need not be the same, and the types of corresponding positions need to be the same |
enumeration | enum | Enumeration types are used to define a collection of values. |
void | void | Used to identify the type of return value of a method, indicating that the method has no return value |
null | null | Indicates that the object value is missing |
undefined | undefined | Used to initialize the variable to an undefined value |
never | never | Never is a subtype of other types, including null and undefined, representing values that never appear |
3, Data type application
1.any type:
As the name suggests, any means anything. TypeScript is a data type used for variables with ambiguous types during programming:
let msg: any = 1 // Number type msg = 'I am who I am' // String type console.log(msg) msg = false // Boolean type console.log(msg) msg = [1, 2] //Array type console.log(msg)
After compilation:
var msg = 1; // Number type msg = 'I am who I am'; // String type console.log(msg); msg = false; // Boolean type console.log(msg); msg = [1, 2]; //Array type console.log(msg);
2.number type:
number represents the data type of numbers. In ts, we can represent not only the common ten digits, but also our hexadecimal representation. Let's take a look:
let number1: number = 0b1010 // Binary let number2: number = 0o744 // octal number system let number3: number = 6 // decimal system let number4: number = 0xf00d // hexadecimal console.log(number1, number2, number3, number4)
After compilation:
var number1 = 10; // Binary var number2 = 484; // octal number system var number3 = 6; // decimal system var number4 = 0xf00d; // hexadecimal console.log(number1, number2, number3, number4);
3.string type:
Characters can be represented by '' or '' and backquotes (` `) to define multiline text and embedded expressions:
let names: string = 'Glory of Kings' let years: number = 10 let words: string = `This year is ${name} release ${years + 1} anniversary` console.log(words)
After compilation:
var names = 'Glory of Kings'; var years = 10; var words = "\u4ECA\u5E74\u662F " + names + " \u53D1\u5E03 " + (years + 1) + " \u5468\u5E74"; console.log(words);
4.Array type:
Arrays can be declared in two forms:
(1).[]:
// Add [] after the element type let arry1: number[] = [1, 2]
(2) . array generics:
// Or use array generics (type customization, this article is just an example) let arry2: Array<number> = [1, 2]
// Add [] after the element type let arry1: number[] = [1, 2] // Or use array generics (type customization, this article is just an example) let arry2: Array<number> = [1, 2] console.log(arry1, arry2)
After compilation:
// Add [] after the element type var arry1 = [1, 2]; // Or use array generics (type customization, this article is just an example) var arry2 = [1, 2]; console.log(arry1, arry2);
5. Tuple:
As mentioned earlier, tuple type is used to represent an array with known number and type of elements. The types of each element need not be the same, and the types of corresponding positions need to be the same
Error instance:
let msg: [string, number]; msg = ['Glory of Kings', 11]; // Normal operation msg = [11, 'Glory of Kings']; // report errors console.log(msg[0]); // output
Then we will report an error when compiling:
correct:
let msg: [string, number]; msg = ['Glory of Kings', 11]; // Normal operation console.log(msg[0]); // output
After compilation:
var msg; msg = ['Glory of Kings', 11]; // Normal operation console.log(msg[0]); // output
6. Enumeration:
enum Color {Red, Green, Blue}; let c: Color = Color.Blue; console.log(c); // Output 2
After compilation:
var Color; (function (Color) { Color[Color["Red"] = 0] = "Red"; Color[Color["Green"] = 1] = "Green"; Color[Color["Blue"] = 2] = "Blue"; })(Color || (Color = {})); ; var c = Color.Blue; console.log(c); // Output 2
7.void:
function hello(): void { console.log("Hello World"); }
After compilation:
function hello() { console.log("Hello World"); }
8.null:
Null in JavaScript, null means "nothing". Null is a special type with only one value. It means an empty object reference. Use typeof to detect null and return object.
9.undefined:
In JavaScript, undefined is a variable with no value set. typeof a variable with no value will return undefined. Null and undefined are subtypes of any other type (including void), which can be assigned to other types, such as numeric types. At this time, the assigned type will become null or undefined. Strict null verification is enabled in TypeScript (– strictNullChecks) attribute, so that null and undefined can only be assigned to void or its corresponding type
10.never:
Never is a subtype of other types (including null and undefined) and represents a value that will never appear. This means that variables declared as never can only be assigned by never. In functions, it usually throws an exception or cannot be executed to the termination point (such as infinite loop)
let x: never; let y: number; // Operation error, numeric type cannot be converted to never type x = 123; // If the operation is correct, the never type can be assigned to the never type x = (()=>{ throw new Error('exception')})(); // If the operation is correct, the never type can be assigned to the numeric type y = (()=>{ throw new Error('exception')})(); // A function with a return value of never can throw an exception function error(message: string): never { throw new Error(message); } // A function with a return value of never can be an end point that cannot be executed function loop(): never { while (true) {} }
After compilation:
var x; var y; // Operation error, numeric type cannot be converted to never type x = 123; // If the operation is correct, the never type can be assigned to the never type x = (function () { throw new Error('exception'); })(); // If the operation is correct, the never type can be assigned to the numeric type y = (function () { throw new Error('exception'); })(); // A function with a return value of never can throw an exception function error(message) { throw new Error(message); } // A function with a return value of never can be an end point that cannot be executed function loop() { while (true) { } }