Typescript Series 2-manuals-Basic types

Source: Internet
Author: User

base type (basic Types)

We provide some basic types, such as numbers (numbers), strings (strings), structs (structures), Boolean values (Boolean), and so on. In Typescript, we support the same types in JavaScript, and we also provide enumeration types.

Boolean

The most basic data types are true/false values, and JavaScript and typescript and other languages call it the ' Boolean ' type.

var Isdone:boolean = false;
Number

As with JavaScript , all the numbers in TypeScript are floating-point. These floating-point numbers use the ' number ' type.

var height:number = 6;
String

JavaScriptUsed to build Web pages and server-side programs.Another basic part is processing text data (textual). With theIn other languages, we use ' string ' to refer to these text data types.As in JavaScript ,typescript also uses double quotation marks (") or single quotation marks (') to wrap the string data.

var name:string = "Bob"; name = ' Smith ';
Array

typescript, like JavaScript, allows you to use arrays. The array type can be written in 2 different ways. In the first way, you can follow an element type followed by a ' [] ' to represent an array of that element type:

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


The second way is to use the array generics, ARRAY<ELEMTYPE>:

var list:array<number> = [1, 2, 3];
Enum

The "enum" type is added outside of the JavaScript standard data type collection. Like languages such as C #, an enum is a more friendly way to represent a group of values.

Enum Color {Red, Green, Blue};var c:color = Color.green;


Enumeration types are counted by default starting from 0. However, you can change this behavior by setting a value on one of the members. For example , for the preceding example, the modification is counted starting from 1 instead of starting from 0.

Enum Color {Red = 1, Green, blue};var c:color = Color.green;


Alternatively, set each of the values in the enumeration manually:

Enum Color {Red = 1, Green = 2, Blue = 4};var C:color = Color.green;


Enumeration Type A handy feature is the name of the value that can be found based on the number. For example, to know which of the value "2" maps to a color enumeration, you can find the corresponding name according to the following code:

Enum Color {Red = 1, Green, Blue};var colorname:string = Color[2];alert (colorname);
any

When we are programming, we sometimes have to describe variable types that we are not sure about. These values may come from dynamic content, such as from user customizations or third-party libraries. In these cases, we want them to not participate in type detection, thus passing the compile detection. to do this, we mark the ' any ' type:

var notsure:any = 4;notsure = "Maybe a string instead"; notsure = false; Okay, definitely a Boolean


The ' any ' type provides a way to collaborate with existing JavaScript, allowing you to gradually choose to participate and not participate in compile-time type detection. If you know some parts of the type, but do not know the other parts of the type, then the ' any ' type is very convenient. For example, there are arrays with different data types :

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

In some cases, the inverse of the ' any ' type may be the ' void ' type, which represents a non-any type. You can think of it as a return type without a return value function:

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

[1] Http://www.typescriptlang.org/Handbook#basic-types


Typescript Series 2-manuals-Basic types

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.