TypeScript Introductory Tutorial 1, basic data types

Source: Internet
Author: User

Reprint: HTTPS://GITHUB.COM/MYERPSOFT/TYPESCRIPT-HANDBOOK/BLOB/MASTER/PAGES/ZH-CHS/BASIC%20TYPES.MD

Overview

To make the program easier to use, we are compatible with several of the most basic data types: numbers (numeric), strings (string), structures (struct), Boolean (Boolean), and so on. In TypeScript, we support almost as many types as Javascript, and we have added useful enumeration types.

Boolean Boolean value

The most basic data types are simple true (True)/false (false), which is referred to as "Boolean" in Javascript and TypeScript (and other languages).

False
Number numbers

As with Javascript, all the number in TypeScript is a floating-point value. TypeScript supports both binary and binary types in addition to 16 and decimal in ECMAScript 2015.

6;     0x9837abdef;  = 0b0010; = 0o74563;  
String strings

When creating a Web page or application using JavaScript, the basic function is to work with strings, and like other languages, we use a "string" type to represent those textual data. Like JavaScript, TypeScript also uses double or single quotes around string data.

' Smith ';

You can also use template strings , which can support multiple lines of text and inline expressions. These strings are enclosed in single quotation marks ( ` ), and the embedded expression ${ expr } is represented in this form.

' Gene '; The     PNs;} . } years old next month. '    

The above expression is equivalent to the following declaration method:

".        "years old next month."
Array arrays

TypeScript, like Javascript, allows you to use arrays. The definition of an array type can be written in two ways. In the first notation, you add ' [] ' after the array element type to indicate that this is an array of that type:

= [3];

The second notation uses a common array type to represent the,array< array element type;

array<= [3]; 
Tuple tuples

A tuple type allows you to express a fixed number of known types of collections, but these types do not have to be the same. For example, you might want to represent the type of a ' string number ' combination:

Declares a tuple type  x: [number];  = [//accurate = [//Error     

We can use numbers to retrieve a known element, but we need to be aware of the correct type.

Console.log (x[0].substr (//correct console.log (x[1].substr (//error, ' number ' type does not have ' substr ' method   

When the index accessed exceeds the boundary, the union type processing is used:

x[//correct, string is allowed to be assigned to (string | number) Console.log (x[//Correct, ' string ' and ' number ' have the ToString method x[  Error, Boolean not one of (string | number)  

The syndication type is a more advanced topic that we'll cover in a later section.

Enum enum

Typescript extends the standard set of JavaScript native data types, adding enum types (enums). Enumerations are a useful type of data, just like in languages such as C #, which provides a way to give values of numeric types and to set up easy-to-distinguish names.

Color {Red, Green, Blue};   = color.green;

By default, the enumeration type marks its elements starting from the number 0. We can change the default value by artificially setting the value of the element. For example, the above example can be set to count starting from 1:

1, Green, Blue};   = color.green;

We can even set values for all the enumeration elements:

4};   = color.green;

Enumeration types have a handy feature, and we can also directly use numeric values to find the names of their corresponding enumeration elements. For example, if we have a value of 2, but we are not sure what element of the enumeration type this value corresponds to, then we can look directly at the name of the value:

1, Green, Blue};    = color[2];alert (colorname); 
Any

When we write an application, we may need to describe some types of ambiguous variables. Because the values of these variables may originate from some dynamic content, such as a user or a library provided by a third party. In this case, we need to skip the type checking for these variables and let them pass the compile-time check directly. For this purpose, we can identify them as ' any ' type:

Okay, definitely a Boolean

Using the ' any ' type is a powerful way of handling our existing JavaScript code, which we can use to gradually increase or decrease the type checking during compilation. As with other programming languages, you might expect to use it Object to implement this functionality, but note that in JavaScript, the Object type simply allows assigning arbitrary values to him, but cannot invoke any of his existing or possible methods:

No problem, toFixed is the real way (but the compiler doesn't verify the accuracy)//Error, no, no, No.

Using ' any ' can provide a lot of convenience when we know a type of partial data type but are unsure of all data types. For example, if you have an array, but the elements in this array belong to different data types, you can do this:

= ["free"];list[; 
Void

voidJust like any the opposite: Void is not, any is all. A function that has no return value can be considered a ' void ' type:

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

It is not recommended to declare a variable to be a void type, because the variable can only be assigned undefined or null .

Undefined
Thanks for translation
    • Oyyd Https://github.com/oyyd/typescript-handbook-zh
    • Ntesmail Https://github.com/ntesmail/Typescript-Handbook
    • Writing a Life Https://github.com/MyErpSoft/TypeScript-Handbook

TypeScript Introductory Tutorial 1, basic data 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.