Typescript Study Notes (i) __typescript

Source: Internet
Author: User
I. Understanding Typescript Typescript is a set of programming languages developed by Microsoft, which is referred to as JavaScript superset, and people who have seen some TS code may obviously be aware of its difference from JS, such as TS added static type checking, Interface interface type definition and object-oriented features, first look at a demo bar. of course, you need to configure your running environment ahead
NPM install-g typescript
Global installation via NPM create a file with a file suffix of TS
Demo.ts
function Howold (year:number) {return
    ' I am ' + year;
}

Let Peopleyear:number =;
Console.log (Howold (peopleyear));//i am 23
Enter Command
TSC Demo.ts
you can find a file named Demo.js in the current file's sibling directory, and open the file to see
function Howold (year) {return
    ' I am ' + year;
}
var peopleyear = n;
Console.log (Howold (peopleyear));
This code we are very familiar with, in fact, after the completion of the final compilation of our reference is also the suffix JS files, rather than TS files. So what are the advantages and disadvantages of TS and JS? For TS, it uses static type detection or interface to set the type interface, which can help us avoid some errors caused by type uncertainty, especially when developing large projects, and the number of errors caused by data types increases greatly. But there is no performance relative to JS too much, and everyone more dependent on JS is often because of its flexibility. two. Data type for Typescript, it retains the basic data types we can use in JavaScript, as well as comparing JavaScript to typescript. Boolean
Typescript writing let
Data:boolean = true;
JavaScript notation let
data = true;
Number typescript with JavaScript all numbers are floating points, as well as binary, octal, decimal, and hexadecimal.
Typescript writing let
data1:number = 0b1010; 
Let data2:number = 0o12;    
Let Data3:number = ten;    
Let data4:number = 0xa;    
JavaScript writing
var data1 = ten;
var data2 = ten;
var data3 = ten;
var data4 = 0xa;
String JavaScript rules the string with "" or "", of course typescript also retained this, on the basis of TS also support the use of (') to introduce text.
Typescript writing let
str1:string = ' typescript ';
Let str2:string = ' Hello ${str1}. '
JavaScript notation
var str1 = ' typescript ';
var str2 = "Hello" + str1 + ".";
Array
Typescript writing let
arr1:number[] = [1,2,3];
Let arr2:array<number> = [1,2,3];
JavaScript notation
var arr1 = [1, 2, 3];
var arr2 = [1, 2, 3];
where the <> is written is the type of the array element. META Group The tuple type is used to represent an array of known elements and types, and the data and type of the element require one by one correspondence, otherwise an error will be made.
Typescript writing let
 data: [String,number];
 data = [' Lilu ', n];
 JavaScript writing
 var data;
 data = [' Lilu ', 23];
Enumeration
Typescript writing
enum Color {Red, Green, Blue} let
c:color = Color.green;
JavaScript writing
var Color;
(function (Color) {
    color[color["red"] = 0] = "red";
    color[color["green"] = 1] = "green";
    Color[color["Blue"] = 2] = "Blue";
}) (Color | | (Color = {}));
var c = Color.green;
The default start ordinal for an enumeration is 0, which can, of course, be assigned manually. Any type any, when you do not know the type of the variable, use the
Typescript writing let
data:any = 1;
data = ' haha ';
data = true;
JavaScript notation
var data = 1;
data = ' haha ';
data = true;
Any makes the variable type flexible, just like a variable declaration in JS, because the any bypasses type checking during compilation. Of course TS also allows you to do this when you are unsure of the array element type:
Typescript writing let
arr:any[] = [1,true, ' haha '];
JavaScript notation
var arr = [1, True, ' haha '];
Empty type Void, which represents an empty type, is generally used as the return type of a function, such as:
Typescript notation
function SayHello (name:string): void{
    console.log (' hello!${name} ');
}
SayHello (' Lilu ');
JavaScript notation
function SayHello (name) {
    Console.log ("hello!" + name);
}
SayHello (' Lilu ');
of course, you can also assign a value to a variable with void, but the variable must be null or undefined.
Typescript writing let
data:void = null;
data = undefined;
JavaScript notation
var data = null;
data = undefined;
Never never represents a variable that never has a value.
Typescript writing let
a:never;
Let b:string;
Let C:never;
A = ' Hello ';
b = A;
A = C;
Description The never type can be assigned to a variable of any type, but a variable of type never can only be assigned a value of type never. These are the syntax descriptions for some of the basic data types, but the data types we often encounter in development are nested, such as:
let data = [
    {
        name: ' Tom ',
        age:10
    },{
        name: ' Mike ',
        age:15
    }
];
in this case, if only the base type is not enough, you need to refer to interface to handle it. Interface first create the data type of an object
Interface obj{
    name:string,
    age:number
}
Next, create an array type.
Interface obj{
    name:string,
    age:number
} let
arr:array<obj> = [
    {
        name: ' Tom ',
        age:10
    },
    {
        name: ' Mike ',
        age:15
    }
];
of course the interface type also allows inheritance, such as I need to add some data types in the following work:
Interface obj1{
    name:string,
    age:number
}
interface Obj2
extends obj1{hobby:string Let
arr:array<obj2> = [
    {
        name: ' Tom ',
        age:10,
        hobby: ' reading '
    },
    {
        name: ' Mike ',
        age:15,
        Hobby: ' Coding '
    }
]

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.