Typescript Basic data types

Source: Internet
Author: User
Tags data structures numeric

For the program we need basic data units, such as: numbers, strings, structures, Boolean and other data structures. In typescript we support many of the data type systems that you expect to have in JavaScript.

Boolean

In JavaScript and typescript, there is also the most basic logical assertion value true/false, using the ' Boolean ' type.

1
var Isdone:boolean = false;
Number

All numeric types, such as Javascript,typescript, have a floating-point count with type ' number '.

1
var height:number = 6;
String

The most basic function of a JavaScript or server-side application in webpages is to process text data. Most of the other languages use ' string ' to represent the text data type. Typescript, like JavaScript, wraps text data in double quotation marks (") or single quotes.

1
2
var name:string = "Bob";
Name = ' Smith ';
Array

In Typescript, as JavaScript allows us to combine operations. The array type can be used in one of two ways.

In the first way, you can take ' [] ' after the data type:

1
var list:number[] = [1, 2, 3];
The second way, you can also take a generic type of array:

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

Typescript adds a collection data type that enumerates this standard for JavaScript. As in C #, enumerations are a more friendly set of names for a set of numeric types:

1
2
Enum Color {Red, Green, Blue};
var c:color = Color.green;
The default enumeration type actually starts with a value of 0, and you can manually set a value for a member. For example, we can set the starting value of the above as 1:

1
2
Enum Color {Red = 1, Green, Blue};
var c:color = Color.green;
or manually set all the enumeration members:

1
2
Enum Color {Red = 1, Green = 2, Blue = 4};
var c:color = Color.green;
Enumeration types can and are easily obtained from a numeric type for the corresponding enumeration name. For example, we have a numeric type of 2, but we do not confirm which enumeration member will be matched, so we can use the following:

1
2
3
4
Enum Color {Red = 1, Green, Blue};
var colorname:string = color[2];

alert (colorname);
Any

Sometimes we need to describe something we don't know about Dynamic data types that are written into the app, which can come from third-party users or LIB. Here we want the data not to join the typescript type check, yes this value is checked by compile time. For this we can use the ' any ' type annotation:

1
2
3
var notsure:any = 4;
Notsure = "Maybe a string instead";
Notsure = false; Okay, definitely a Boolean
The ' any ' type is a powerful type system that is compatible with existing JavaScript libraries. He allowed to skip the typescript compile-time type check.

The ' any ' type system is for type systems where we only know part of the data type, but not all data types. such as a mix of multiple types of collection arrays.

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

LIST[1] = 100;
Void

The data type that is relative to ' any ' is ' Void ', which represents no data type. There is no return value for one of our common methods:

1
2
3
function Warnuser (): void {
Alert ("This is my warning message");
}

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.