Home >Web Front-end >JS Tutorial >TypeScript basic types of study notes_javascript skills

TypeScript basic types of study notes_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:54:091029browse

There are 7 basic types in TypeScript.

1. boolean

Copy code The code is as follows:

var isDone: boolean = false;

2, number

represents a number in JavaScript. In JavaScript, both "integers" and "floating-point numbers" are stored as double-precision floating-point types.

Copy code The code is as follows:

var height: number = 6;

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.

Copy code The code is as follows:

var name: string = "bob";
name = 'smith';

4. array

There are two methods of array declaration in TypeScript.

①Use "[]" to declare:

Copy code The code is as follows:

var list: number[] = [1, 2, 3];

② Use array type to declare:

Copy code The code is as follows:

var list: Array = [1, 2, 3];

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.

Copy code The code is as follows:

enum Color {
​ Red,
Green,
Blue
};
var c: Color = Color.Green;

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.

Copy code The code is as follows:

enum Color {
​ Red = 1,
Green,
Blue
};
var c: Color = Color.Green;

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.

Copy code The code is as follows:

enum Color {
​ Red = 1,
Green = 2,
Blue = 4
};
var c: Color = Color.Green;

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:

Copy code The code is as follows:

enum Color {
​ Red = 1,
Green,
Blue
};
var colorName: string = Color[2];
alert(colorName);
colorName = Color[4];
alert(colorName);

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:

Copy code The code is as follows:

var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

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:

Copy code The code is as follows:

var list: any[] = [1, true, "free"];
list[1] = 100;

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.

Copy code The code is as follows:

function warnUser(): void {
alert("This is my warning message");
}

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn