TypeScript Basic Types (basic type)

Source: Internet
Author: User

Before we learn typescript, we need to know how to get typescript writing to work correctly. There are two ways: use Visual Studio and use NodeJs.

Here I choose is nodejs to compile typescript, because my notebook's vs is 2012, in Typescript's official website to see the download is typescript for VS2013 and typescript for VS2015. The amount, incidentally affixed on Typescript's official website, has the need to download.

Then use Nodejs to compile the Typescript method:

// Installing INSTALL:NPM Install -g typescript  compiling compile:tsc your.ts

Using VS's limited to vs versions 2013 and 2015, you can download the relevant components and create a new "Typescriptfile". There will be a. js file under the. ts file, and. JS is the automatically compiled file, and then the HTML references the. js file.

In order for the program to run, we need to use some basic types of data: numbers, strings, structures, Boolean, and so on. Typescript supports these types of data that are used in JavaScript, and also provides useful enumeration types.

Boolean (Boolean type)
The most basic data types are simple true/false, which are called "Boolean" types in JavaScript and typescript (as in other languages).

var isDone:booleanfalse;

Number (numeric)
As in JavaScript, the values in typescript are also floating-point numbers. These floating-point numbers are of type "number".

var height:number = 6;

String (String)
Another important part of JavaScript authoring Web pages or server-side programs is text-type data. As with other languages, we use string literals to refer to data of these literal types. As with JavaScript, Typescript also uses double quotation marks ("") or single quotation marks ("") to contain string data.

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

Array (arrays)
Typescript, like JavaScript, allows arrays to be used to enclose a series of values. There are two different ways to write an array. The first is to use brackets ([]) after the element type to represent an array of this type of element:

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

The second is to use generics to create an array of type array<elemtype/* array elements of type */>:

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

Enum (enum)
In addition to JavaScript's native standard datasets, Typescript adds a useful "enum" type. For example, C #, enumerations give us a more friendly name (number type) to discern the value collection.

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

By default, the elements in the enumeration are numbered starting at 0. You can manually set this value for the element. For example, we are now setting starting at 1 instead of 0 in the original example:

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

Or, even manually setting the values of all enumerated elements is possible:

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

Enumeration types have a handy feature that allows you to find the corresponding element names in an enumeration by numeric values. Let's look at an example, if we have a value of 2, but we don't know which element corresponds to the above enumeration, then we can find the corresponding name:

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

Any
When we write the application, we may need to describe some variables that are ambiguous types. These variables may come from dynamic content, such as user-supplied or third-party libraries. In these cases, we want to jump out of type checking and let these values pass the compile-time check. For this purpose, we use the "any" type to identify these values:

var notsure:any = 4//  is now a string type false//  is now a Boolean type 

The "any" type is good for working with our existing JavaScript code, and it can be used to control whether or not to increase or decrease the type checking of the data at compile time.
If you only know a subset of the data types, not all data types, it is convenient to use the "any" type. For example, you might have arrays, but the elements of this array correspond to different data types.

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

Void
Relative to the "any" type is the "void" type, which represents no type. You may often see it as a function that does not return a value:

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

TypeScript Basic Types (basic type)

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.