In this section, we study TypeScript
language, including: number types, string types, boolean values, arrays, tuples, and so on. TypeScript
supports JavaScript
, and also provides practical enumeration types for us to use.
Number type
And JavaScript
as in, TypeScript
all numbers are floating-point numbers. The type of these floating-point numbers is number
type. In addition to the decimal and hexadecimal literals, the number
type also supports the binary and octal literals introduced in ECMAScript 2015
Example:
Declare numeric variables, which can be in binary, decimal, hexadecimal, or octal representations:
let n1: number = 8; // 十进制
let n2: number = 0xf00d; // 十六进制
let n3: number = 101010; // 二进制
let n4: number = 0o633; // 八进制
console.log(n1, n2, n3, n4);
Compiled into JavaScript
code:
var n1 = 8; // 十进制
var n2 = 0xf00d; // 十六进制
var n3 = 101010; // 二进制
var n4 = 411; // 八进制
console.log(n1, n2, n3, n4);
// 8 61453 101010 411
What is decimal, binary, octal, hexadecimal:
- decimal: decimal number is the number represented by
0
,1
,2
...9
, these ten numbers. - binary: binary data is a number represented by two numbers,
0
and1
Its base is2
, the carry rule is "every two enters one", and the borrow rule is "borrow one as two". - octal: octal is a kind
8
base-counting method using0
,1
,2
,3
,4
,5
,6
,7
eight numbers, every octal1
. - Hexadecimal: Hexadecimal is a way of representing data in a computer. It is not the same as the decimal notation in our daily life. It is generally represented by numbers
0
to9
and lettersA
toF
(or a~f). Any combination of these numbers and letters is used to represent a word between0~15
WhereA~F
represents10~15
, these are called hexadecimal numbers.
Boolean type
The boolean type boolean
represents a logical value, and there are only two values true
and false
Example:
For example, declare two Boolean variables and output:
const b1: boolean = true;
const b2: boolean = false;
console.log(b1, b2);
Compile the above code into JavaScript code:
var b1 = true;
var b2 = false;
console.log(b1, b2);
Output:
true false
String type
The string type string
used to represent the text data type, as in JavaScript
, single quotation marks '
or double quotation marks "
can be used to represent strings.
Example:
Declare a variable of string type:
let str: string = "侠课岛";
console.log(str);
Output:
侠课岛
Array type
There are two ways to represent the array type. The first type is followed by []
brackets 06130ee0648141 after the element type to indicate an array composed of elements of this type.
Example:
For example, a digital type array declaration composed num
, and an array of string type consisting of character
:
// 在元素类型后面加上[],定义数字类型数组
let num: number[] = [1, 2, 3];
console.log(num);
// 定义字符串类型数组
let character: string[] = ['a', 'b', 'c'];
console.log(character);
The second way is to use array generics, the format is Array<element type>.
Example:
For example, declare an array of numeric type:
// 使用数组泛型
let arr: Array<number> = [1, 2, 3];
console.log(arr2);
Compile the above code into JavaScript
code:
// 使用数组泛型
var arr = [1, 2, 3];
console.log(arr2);
Output:
[ 1, 2, 3 ]
Tuple type
A tuple represents an array of known number and type of elements. The elements in a tuple can be of different types, but it should be noted that the number and order of the defined types must correspond to the value of the element one by one, and one less element does not work.
Example:
Declare a tuple, the first element in the tuple is a string type, and the second element is a number type:
// 类型和值必须一一对应
let tuple: [string, number] = ['小飞侠', 18];
console.log(tuple);
console.log(tuple[0]);
Compiled into JavaScript
code:
// 类型和值必须一一对应
var tuple = ['小飞侠', 18];
console.log(tuple);
console.log(tuple[0]);
Output:
[ '小飞侠', 18 ]
小飞侠
Enumerated type
The enumeration type enum
used to define a set of values.
Example:
Declare an enumerated variable Fruits
, this variable has three different values:
enum Fruits {watermelon, Apple, Litchi};
console.log(Fruits);
Compile the code into JavaScript
code:
var Fruits;
(function (Fruits) {
Fruits[Fruits["watermelon"] = 0] = "watermelon";
Fruits[Fruits["Apple"] = 1] = "Apple";
Fruits[Fruits["Litchi"] = 2] = "Litchi";
})(Fruits || (Fruits = {}));
;
console.log(Fruits);
The output after executing the code is:
{
'0': 'watermelon',
'1': 'Apple',
'2': 'Litchi',
watermelon: 0,
Apple: 1,
Litchi: 2
}
any any type
Arbitrary value is TypeScript
for variables whose type is not clear during programming. Variables declared as any
can be assigned any type of value.
Example:
For example, declare a any
variable of type unknown
:
let unknown: any;
unknown = 7; // 数字类型
console.log(unknown);
unknown = 'xkd'; // 字符串类型
console.log(unknown);
unknown = false; // 布尔类型
console.log(unknown);
Compile the above code into JavaScript
code:
var unknown;
unknown = 7; // 数字类型
console.log(unknown);
unknown = 'xkd'; // 字符串类型
console.log(unknown);
unknown = false; // 布尔类型
console.log(unknown);
Output:
7
xkd
false
void type
In Java
such as 06130ee0648515, if a method does not return a value, the default return value type of the method is void
type.
JavaScript
is no such type in 06130ee0648529, this new type of TypeScript
void
can be used to identify the type of the return value of the method, which means that the method has no return value.
Example:
If there is no return value when defining the function, it is recommended to void
after the function name. For example, for the following function show()
, we did not specify a return value for it, so we specify that the return value of this function is of type void
function show(): void {
console.log("你好,侠课岛!");
}
// 调用函数
show();
Compiled into JavaScript
code:
function show() {
console.log("你好,侠课岛!");
}
// 调用函数
show();
Output:
你好,侠课岛!
Null type and Undefined type
null
type and the undefined
type are very similar to the corresponding types in JavaScript
Null
type and the Undefined
, and they are also unique values.
Example:
var n = null;
var u = undefined;
console.log(n, u); // null undefined
Never type
Never
type represents the type of values that never exist.
Example:
When declaring a variable, we can declare the variable as type never
let a: never;
never
type is a subtype of any type, and can also be assigned to any type.
Example:
let a: never;
let b: number;
let c: string;
// never类型可以赋值给never类型
a = (()=>{
throw new Error('xkd');
})();
// never类型可以赋值给number类型
b = (()=>{
throw new Error('xkd');
})();
// never类型可以赋值给string类型
c = (()=>{
throw new Error('xkd');
})();
But no type is a never
or can be assigned to never
type, even any
cannot be assigned to never
.
Example:
For example, assign the value of the number type to the never
type:
let a: never;
a = 100;
// 输出: Type '100' is not assignable to type 'never'
Link: https://www.9xkd.com/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。