Features a superset of JavaScript

“” added static type system static type: Compile time, you will know the type of each variable, because the type error and can’t do is grammar mistake (can do error checking before operation, so the language itself can provide a type error, code hinting, automatic filling are congruent, the IDE can also provide a wide range of services, such as smart rename internal reading code, etc.), fast into the corresponding class dynamic type: You don’t know the type of every variable at compile time, you do type checking and binding at runtime, and what you can’t do because of a type error is a runtime error.

Strong typing: Implicit type conversions are not tolerated. Python "1"+2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Cannot concatenate 'STR' and 'int' objects Weak types: tolerate implicit type conversions. For example, js 1+'a'=' 1A 'weak type, static type: C/C++ weak type, dynamic type: JS/VB/Perl/PHP strong type, static type: Java/C# strong type, dynamic type: Python, Scheme Static explicit type: Java/C static implicit types: Ocaml, HaskellCopy the code

Designed for large-scale software development

TypeScript makes your code more readable and maintainable. The type system is actually the best documentation. Most functions can be found at compile time by looking at the type definition. TypeScript is a superset of JavaScript..js files can be renamed to.ts. Even if you don't explicitly define types, You can make automatic type inferences you can define almost anything from the simple to the complex and even if TypeScript compiles with errors, you can generate JavaScript files that are compatible with third-party libraries, even if they're not written in TypeScript, You can also write separate type files for TypeScript to read. TypeScript has an active community. Most third-party libraries have type definition files for TypeScript TypeScript embraces the ES6 specification. TypeScript's shortcomings have some learning costs, The need to understand concepts like Interfaces, Generics, Classes, enumerated types, and so on that may not be familiar to front-end engineers can add some development costs in the short term because you need to write more type definitions, but for a project that requires long-term maintenance, TypeScript reduces its maintenance costs and integration into the build process takes some work and may not be perfectly integrated with some librariesCopy the code

The final compilation produces JavaScript

Variable declarations support let and const; Let isDone: Boolean = false; Let decLiteral: number = 6; String let name: string = "Bob "; By default null and undefined are subtypes of all types. This means that you can assign null and undefined to variables of type number. undefined let u: undefined = undefined; Null let n: null = null; Array let list: number[] = [1, 2, 3]; Let list: Array<number> = [1, 2, 3]; Let x: [string, number]; X = ['hello', 10] compilation success x = [10, 'hello'] compilation failure When accessing an element with a known index, the correct type is x[0]. Substr (1) compilation success x[1]. Substr (1) compilation failure When accessing an element that is out of bounds, The union type is used instead: x[3] = 'world'; Compile successfully string can be assigned to (string | number) type x [5]. The toString () to compile successfully x [6] = true; Compilation fails Not Boolean (string | number) enumeration type enum type is a complement to JavaScript standard data type by default, starting from 0 for the element number. enum Color {Red, Green, Blue} let c: Color = Color.Green; Console. log(c) // 1 You can also manually specify member values. enum Color {Red = 1, Green = 2, Blue = 4} let c: Color = Color.Green; Console. log(c) // 2 If only one element is set, the other elements are numbered in sequence. Enum Color {Red = 8, Green, Blue} let C: Color = color.green; Enum Color {Red = 1, Green, Blue} let c: string = Color[2]; Console.log (c) //Green Any Sometimes we want to specify a type for variables whose type is not known at programming time. These values may come from dynamic content, such as user input or third-party code libraries. In this case, we don't want the type checker to check these values and just pass them through compile-time checks. Let notSure: any = 4; The any type is useful when rewriting existing code, allowing you to optionally include or remove type checking at compile time. notSure = "maybe a string instead"; NotSure = false; The any type is also useful when you only know the type of part of the data. For example, you have an array that contains different types of data: let list: any[] = [1, true, "free"]; List [1] = 100; Void this means there is no type. When a function returns no value, you can use function warnUser(): void {console.log("This is my warning message"); } declaring a variable of type void is useless because you can only give it undefined and null let unusable: void = undefined; The Never Never type represents the types of values that Never exist. Function error(message: string): never {throw new error(message); } // The inferred return value type is never function fail() {return error("Something failed"); } // function infiniteLoop() must have an unreachable end: Never {the while (true) {}} stt_object the Object in the js declare the function the create (o: Object | null) : void; create({ prop: 0 }); Create (null); // Create (42); // Failed to compile create("string"); // Failed to compile create(false); // failed to compile create(undefined); Type assertion Type assertion is like conversion in other languages, but without special data checking and deconstruction. It has no run-time impact, only at compile time. Type assertions come in two forms. Let someValue: any = "this is a string"; let strLength: number = (<string>someValue).length; Let someValue: any = "this is a string"; let strLength: number = (someValue as string).length; When JSX is used in TypeScript, only AS syntax assertions are allowed.Copy the code