Basic Types of TypeScript learning notes

Source: Internet
Author: User

Basic Types of TypeScript learning notes

TypeScript is a JavaScript superset. After TypeScript is compiled, JavaScript code is generated. TypeScript is typed, which is called TypeScript. Compared with weak JavaScript, typed typescripts are easier to maintain.

There are 7 basic types in TypeScript.

1. boolean

The Code is as follows:

Var isDone: boolean = false;

2. number

Represents a number in JavaScript. In JavaScript, both "integer" and "floating point number" are stored in double-precision floating point type.

The Code is as follows:

Var height: number = 6;

3. string

String. Like JavaScript, a pair of double quotation marks (") or a pair of single quotation marks (') can be used to represent strings.

The Code is as follows:

Var name: string = "bob ";

Name = 'Smith ';

4. array

TypeScript has two array declaration methods.

① Use "[]" to declare:

The Code is as follows:

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

② Use the array type to declare:

The Code is as follows:

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

Both methods can be used, and the effect is not different. However, we recommend that you use only one of them in the code to keep the code style uniform.

5. enum

The enumeration type is newly added to TypeScript, but not in JavaScript.

The Code is as follows:

Enum Color {

Red,

Green,

Blue

};

Var c: Color = Color. Green;

Like C #, if the value of the first item is not declared, the value of Red above is 0, and each item is added with one, that is, Green is 1, and Blue is 2.

The Code is as follows:

Enum Color {

Red = 1,

Green,

Blue

};

Var c: Color = Color. Green;

So the Red value is 1, Green is 2, and Blue is 3.

You can also specify a value for each item.

The Code is as follows:

Enum Color {

Red = 1,

Green = 2,

Blue = 4

};

Var c: Color = Color. Green;

In addition, the enumeration type has a special function. If we have a value but we do not know whether the enumeration type is defined, we can use the following method to obtain it:

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 the value of Green is 2, and no enumerated value is 4, undefined is returned.

6. any

Like the default type of a variable in JavaScript, it is dynamic and can be assigned to any type. For example:

The Code is as follows:

Var notSure: any = 4;

NotSure = "maybe a string instead ";

NotSure = false; // okay, definitely a boolean

When defined as any, the syntax-aware function is lost, which is equivalent to writing JavaScript.

It is worth mentioning that any can be used with Arrays:

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, indicating that the function does not return any value.

The Code is as follows:

Function warnUser (): void {

Alert ("This is my warning message ");

}

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.