TypeScript-Basic Type System
For programs, we need basic data units, such as numbers, strings, structures, and boolean. In TypeScript, we support many data type systems that you expect in JavaScript. Boolean also has the most basic logical asserted values true/false in JavaScript and TypeScript, and adopts the 'boolean' type. Var isDone: boolean = false; Number, such as JavaScript, All numeric types of TypeScript use floating point type count, its type is 'number '. Var height: number = 6; the most basic function of String in webpages JavaScript or server applications is to process text data. In most other languages, 'string' is used to represent the text data type. Similar to JavaScript, TypeScript uses double quotation marks (") or single quotes to enclose text data. Var name: string = "bob"; name = 'Smith '; Array allows us to perform combined operations like JavaScript in TypeScript. The array type can be either of the following two methods. First, you can add '[]': var list: number [] = [1, 2, 3] after the data type. Second, you can also use a Generic Array type: var list: Array <number> = [1, 2, 3]; Enum TypeScript adds a standard set data type such as enumeration to JavaScript. Like in c #, enumeration is a more friendly group of values: enum Color {Red, Green, Blue}; var c: Color = Color. green; the value of the default Enumeration type starts from 0. You can manually set the value of a member. For example, we can set the starting value to 1: enum Color {Red = 1, Green, Blue}; var c: Color = Color. green; or manually set all enumeration members: enum Color {Red = 1, Green = 2, Blue = 4}; var c: Color = Color. green; the enumerated type can obtain the corresponding enumerated name easily from a numeric type. For example, if we have a value of Type 2 but do not confirm which enumeration member will be matched, we can use enum Color {Red = 1, Green, Blue}; var colorName: string = Color [2]; alert (colorName); Any sometimes needs to describe something we don't know about the dynamic data types written into the application, which may come from third-party users or lib. Here we want this data not to be added to the TypeScript type check. Yes, this value passes the compile-time check. For this reason, we can use the 'any' type annotation: var notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean 'any' type is a powerful type system compatible with existing JavaScript libraries. It is allowed to skip the type check during TypeScript compilation. The 'any' type only knows some data types, but not all data types. For example, a collection array mixed with multiple types. Var list: any [] = [1, true, "free"]; list [1] = 100; the data type relative to Void and 'any' is 'void ', it indicates that there is no data type. One of our common methods does not return any values: function warnUser (): void {alert ("This is my warning message ");}