This is the 7th day of my participation in Gwen Challenge

Initial TypeScript

1. The TypeScript definition

TypeScript is a superset of JavaScript types, called TS for short. It includes JavaScript syntax and supports ES6 standards, so be sure to learn ES6 before learning TypeScirpt. TypeScript supports type definitions, classes, interfaces, enumerations, generics, etc., so it is designed for large applications that can be compiled into pure JavaScript and run in any browser.

Microsoft released the first public version of TypeScript in October 2012 and released TypeScript on June 19, 2013 after a preview release

The author of TypeScript is anders hejlsberg, the chief architect of C#

2. The TypeScript

  • Compiled JavaScript

TypeScript compiles clean, concise JavaScirpt code that runs in any browser, node.js environment, and any JavaScirpt engine that supports ECMScirpt3(or later).

  • The type system

A type system (static typing) can detect errors as developers write scripts, and finding and fixing errors is a pressing need for today’s teams. With this capability, developers can write readable and maintainable code.

  • Develop large projects

Sometimes you need to make small incremental changes to your code base to improve your project. These small changes can have serious and unintended consequences, so it is necessary to reverse them. Refactoring with TypeScript tools is easy and fast.

  • Better collaboration

When developing large projects, where there are many developers, all kinds of problems and bugs arise. Type system detection is the first feature to detect errors during coding, rather than during compilation, the development team created a more efficient coding and debugging process.

Second, get started quickly

1. The TypeScript installation

npm i -g typescript  / / installation
tsc -v  // Check the ts installed version
Copy the code

2. Data type and inference of TS

TypeScript adds constraint footer type annotations when declaring variables as functions. The basic data types and reference types supported are: String, number, Boolean, null. Undefined, symbol, array, object, Function, any, void. Void can be used to identify variables and Function return values.

  • Type constraints
let hello:string = 'hello world'
hello = 2 // this code was written with vscode error. Hello is a character type and cannot be assigned a numeric type
let message:any = 'anything'
message = true

console.log(hello);
Copy the code

  • Type inference
let age = 30 Age is a numeric type. Assigning a character type again will cause an error
age = 'zhangsan'
Copy the code

  • Compile the ts – > js
tsc  ./1.dataType.ts  // After execution, a js with the same name will appear
Copy the code

  • Run the node xx. Js

  • Run ts directly
ts-node xx.js
Copy the code

!!!!!!!!! Step on the pit, if error is reported

$ ts-node 1.dataType.ts
Error: Cannot find module '@types/node/package.json'
Copy the code

Run the following code

npm install -d tslib @types/node

Copy the code

Run, solve