Original source:
Http://www.typescriptlang.org/Handbook
Based on the needs of web development and the worship of the nerves, intends to learn about typescript.
The ability is limited, basically belongs to the translation on the basis of oneself cognition, the mistake inevitably, mainly is through the translation to study.
Basic type
In programming we need to use some simple data elements, such as: numeric, string, structure, Boolean, and so on.
As you wish, the data types in JavaScript are supported by Typescript,
With a convenient enumeration type thrown on to help things along. (This sentence is not understood for the time being.)
Boolean Type (Boolean)
The most basic data types are simple true or false, defined as ' Boolean ' in JavaScript and typescript (and other languages).
var Boolean false;
Numeric type (number)
Like JavaScript, all numeric types in typescript are floating-point types. These floating-point type values are defined as ' number '.
var height:number = 6;
Strings (String)
When writing Web pages and server programs in JavaScript, another basic part is working with text data.
Like in other languages, we use ' string ' to define the text data type, and as with JavaScript, Typescript also uses double quotation marks (")
or single quotation marks (') enclose the string data.
var name:string = "Bob"= ' Smith ';
Arrays (Array)
Typescript, like JavaScript, allows you to use arrays. An array type can be defined in either of two ways.
First, you can represent an array of the specified element type by enclosing the element's type in brackets []:
var list:number[] = [1, 2, 3];
Second, use the common array type,array< element type;:
var list:array<number> = [1, 2, 3];
Note: I personally like the second kind.
Enum (enum)
' enum ' is a useful extension of the standard collection type in JavaScript. Like other languages, such as C #, enumerations can define a more friendly name for the value of a collection type.
enum Color {Red, Green, Blue}; var c:color = Color.green;
By default, the ordinal of an enumerated element is starting with 0. We can set the ordinal number of the element by hand. For example, we can use 1 instead of the starting element of 0:
Enum Color {Red = 1, Green, Blue}; var c:color = Color.green;
Alternatively, we can manually set the ordinal number of each element:
Enum Color {Red = 1, Green = 2, Blue = 4}; var c:color = Color.green;
We can get the name of the element by enumerating the value of the element, for example, if we know the value of the element (note: ordinal) 2, but it is not sure which element in the enumeration matches,
We can find its name by value:
Enum Color {Red = 1, Green, Blue}; var colorname:string = color[2];alert (colorname);
Variant (Any)
When we write a program, we may need to define a variable that does not yet know the specific type. These values may come from dynamic content, such as from a user or a third-party library.
In these cases, we choose to exit the type check and let the value pass the compile-time check. At this point, we mark by ' any ':
var notsure:any = 4= "Maybe a string instead"false// Okay, definitely a Boolean
The any type is a powerful method in existing JavaScript that allows you to gradually select and exit type checking during compilation.
Note: It seems to me to be a weak type of JavaScript compatible.
The any type is useful when you might just know a certain part of the type. For example, you might define an array, but the array is mixed with different types:
var true, "free"];list[1] = 100;
Note: A variant like Delphi, or TValue.
No type (Void)
Sometimes, an any type does not accurately represent ' none ', that is, there is no type of any type and cannot be represented by a class of type.
You can usually treat this as a function that returns a value that does not return:
function void {alert ("This is my warning message");}
TypeScript Handbook basic type (translation)