The basic types of typescript learning notes

Source: Internet
Author: User
Tags numeric value

Typescript is a superset of JavaScript, and typescript generates JavaScript code after compilation. The biggest feature of typescript is the type, so it's called typescript. Typed typescript appear to be easier to maintain than weak type JavaScript.

There are a total of 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, either "integer" or "floating-point number" is stored as a double-precision floating-point type.

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.

The code is as follows:

var name:string = "Bob";

Name = ' Smith ';

4, array

There are two types of array declaration methods in typescript.

① uses "[]" to declare:

The code is as follows:

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

② uses an array type to declare:

The code is as follows:

var List:array = [1, 2, 3];

Both declarations are available and the effect is not different. But the recommended code should try to use only one of them to keep the code consistent.

5. Enum

The enumeration type is newly added in typescript and is not in JavaScript.

The code is as follows:

Enum Color {

Red,

Green,

Blue

};

var c:color = Color.green;

Like C #, if you don't declare the value of the first item, the Red value above is 0, and then each item is incremented by one, that is, Green is 1,blue 2.

The code is as follows:

Enum Color {

Red = 1,

Green,

Blue

};

var c:color = Color.green;

So at this point Red's value is 1,green to 2,blue 3.

Of course, 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;

There is also a special function for enumeration types, if we have a numeric value, but we do not know if there is a definition in the enumeration type, you can get it in the following ways:

The code is as follows:

Enum Color {

Red = 1,

Green,

Blue

};

var colorname:string = color[2];

alert (colorname);

ColorName = color[4];

alert (colorname);

Then the Green and undefined will be exported. Because Green's value is 2, and no enumeration defines a value of 4, it returns undefined.

6, any

As with the default type of variables in JavaScript, the reference is dynamic and can be given 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, it loses the function of syntactic perception, which is equivalent to writing JavaScript.

It is worth mentioning that any can be used in conjunction with an array:

The code is as follows:

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

LIST[1] = 100;

7, void

This type can be used only in functions, and the return type of the function can be specified as void, indicating that the function does not return any values.

The code is as follows:

function Warnuser (): void {

Alert ("This are my warning message");

}

The above mentioned is the entire content of this article, I hope you can enjoy.

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.