# TypeScript è¯æ³åºç¡ä¸å·¥ç¨å®è·µ TypeScript æ¯ç± MicroSoft åºåç JavaScript è¶ éï¼å®å¨å ¼å®¹ JavaScript çææç¹æ§çåºç¡ä¸ï¼é带äºéæç±»åçæ¯æï¼TypeScript è¿å 许æä»¬ä½¿ç¨å°æªæ£å¼åå¸ç ECMAScript çè¯è¨ç¹æ§ï¼å¨ç¼è¯æ¶è¿è¡ç±»ä¼¼äº Babel è¿æ ·çé级转åãJavaScript æ¬èº«ä¹å¨æç±»åçè¯è¨ï¼å³æ¯å¨è¿è¡æ¶æè¿è¡ç±»åæ ¡éªï¼è¯¥ç¹æ§èµäºäºå ¶å¿«éåååçè½åï¼å´å¨æå»ºå¤§å JavaScript åºç¨æ¶åæä¸é®ï¼å ¶æ æ³å¨ç¼è¯æ¶å¸®å©è§é¿å¯è½çç±»åé误ï¼ä¹æ æ³å©ç¨èªå¨è¡¥å ¨ãèªå¨éæçå·¥å ·ç¹æ§ãTypeScript çéæç±»åç¹æ§å叮婿们å¨ç¼è¯æ¶å°½å¯è½è§é¿ç±»åé误ï¼å¹¶ä¸ TypeScript ä¼å°½å¯è½å°ä»ä¸ä¸æä¿¡æ¯ä¸è¿è¡ç±»åæ¨å¯¼ï¼ä»¥é¿å å Java çéæç±»åè¯è¨ä¸è¿äºåä½ç麻ç¦ã ç®åï¼Angular å·²ç»ä½¿ç¨ TypeScript éæäºä»£ç ï¼å¦ä¸å¤§åç«¯æ¡æ¶ Vue çæ°çæ¬ä¹å°ä½¿ç¨ TypeScript è¿è¡éæãå¨å¯é¢è§çæªæ¥ï¼TypeScript å°æä¸ºå端å¼åè å¿ é¡»ææ¡çå¼åè¯è¨ä¹ä¸ã - ç±»åæ£æ¥ãTypeScript ä¼å¨ç¼è¯ä»£ç æ¶è¿è¡ä¸¥æ ¼çéæç±»åæ£æ¥ï¼è¿æå³çä½ å¯ä»¥å¨ç¼ç é¶æ®µåç°å¯è½åå¨ç鿣ï¼èä¸å¿ æå®ä»¬å¸¦å°çº¿ä¸ã - è¯è¨æ©å±ãTypeScript ä¼å æ¬æ¥èª ES 6 åæªæ¥ææ¡ä¸çç¹æ§ï¼æ¯å¦å¼æ¥æä½åè£ é¥°å¨ï¼ä¹ä¼ä»å ¶ä»è¯è¨åé´æäºç¹æ§ï¼æ¯å¦æ¥å£åæ½è±¡ç±»ã - å·¥å ·å±æ§ãTypeScript è½å¤ç¼è¯ææ åç JavaScriptï¼å¯ä»¥å¨ä»»ä½æµè§å¨ãæä½ç³»ç»ä¸è¿è¡ï¼æ éä»»ä½è¿è¡æ¶çé¢å¤å¼éãä»è¿ä¸ªè§åº¦ä¸è®²ï¼TypeScript æ´åæ¯ä¸ä¸ªå·¥å ·ï¼è䏿¯ä¸é¨ç¬ç«çè¯è¨ã - TypeScript è¿å¯ä»¥å¸®å©å¢ééå¡âç±»åæç»´âï¼æ¥å£æä¾æ¹å°è¢«è¿«å»æè API çè¾¹çï¼ä»ä»¬å°ä»ä»£ç çç¼åè èå为代ç ç设计è ã  # ç¯å¢é ç½® å¯ä»¥åè [fe-boilerplates](https://github.com/wx-chevalier/fe-boilerplates) æè [Backend-Boilerplates](https://github.com/wx-chevalier/Backend-Boilerplates)ï¼å¦ææ³äºè§£ TypeScript å¨åå端å¼åä¸çåºç¨ãæä»¬ä»¬å¯ä»¥éè¿ npm å®è£ TypeScript çä¾èµå ï¼ ```sh # å ¨å±å®è£ $ npm install -g typescript # æ£æµæ¯å¦å®è£ æå $ tsc -v Version 2.8.3 ``` TypeScript æºæä»¶ä¸è¬ä½¿ç¨ `.ts` æè `.tsx` 为åç¼ï¼å ¶å¹¶ä¸è½ç´æ¥è¿è¡å¨æµè§å¨ä¸èéè¦è¿è¡ç¼è¯è½¬åï¼TypeScript ç宿¹æä¾äº `tsc` å½ä»¤æ¥è¿è¡æä»¶ç¼è¯ï¼ ```sh $ tsc main.ts # åæ¶ç¼è¯å¤ä¸ªæä»¶ $ tsc main.ts worker.ts # ç¼è¯å½åç®å½ä¸çå ¨é¨ ts æä»¶ï¼å¹¶ä¸ä¼éå½ç¼è¯ $ tsc *.ts # å¯å¨åå°å¸¸é©»ç¼è¯ç¨åº $ tsc main.ts --watch ``` å¨å®é ç项ç®ä¸ï¼æä»¬ä¹å¾å¾ä¼å¨é¡¹ç®æ ¹ç®å½é ç½® tsconfig.json æä»¶ï¼æ¥ä¸ªæ§åé ç½® TypeScript çç¼è¯åæ°ï¼ ```json { "extends": "@m-fe/tsconfig/tsconfig.json", "compilerOptions": { "incremental": true, /* Basic Options */ "target": "ES2016" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ // "declaration": true, /* Generates corresponding '.d.ts' file. */ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "build" /* Redirect output structure to the directory. */, // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */ // "importHelpers": true, /* Import emit helpers from 'tslib'. */ // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ "strict": true /* Enable all strict type-checking options. */, // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* Enable strict null checks. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ /* Additional Checks */ // "noUnusedLocals": true, /* Report errors on unused locals. */ // "noUnusedParameters": true, /* Report errors on unused parameters. */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ /* Source Map Options */ // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ /* Experimental Options */ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ } } ``` ä¹å¯ä»¥ä½¿ç¨ [ts-node](https://github.com/TypeStrong/ts-node) å¿«éå°ç´æ¥è¿è¡ TypeScript æä»¶ï¼ ```sh # Execute a script as you would normally with `node`. ts-node script.ts # Starts the TypeScript REPL. ts-node # Execute code with TypeScript. ts-node -e 'console.log("Hello, world!")' # Execute, and print, code with TypeScript. ts-node -p '"Hello, world!"' # Pipe scripts to execute with TypeScript. echo "console.log('Hello, world!')" | ts-node ``` # ç¼è¯é ç½® å¨ tsconfig.json ä¸ï¼æä»¬å¯ä»¥èªå®ä¹å¾å¤çç¼è¯é ç½®é¡¹ï¼æ¬èæä»¬å³è®¨è®ºæäºå ¸ååºæ¯ä¸çé ç½®æ¡ä¾ã # Links - https://mp.weixin.qq.com/s/yK0ll0MygWrChYNrthTwxw - https://github.com/David-Else/modern-typescript-with-examples-cheat-sheet