文章目次
1 安装VS Code
https://code.visualstudio.com/
安装VS Code插件:
2 安装Node.js
安装与当前VS Code(1.52.1)使得Node.js版本(Help->About),方便后续VS Code插件开辟:
- Version: 1.52.1 (system setup)Commit: ea3859d4ba2f3e577a159bc91e3074c5d85c0523Date: 2020-12-16T16:34:46.910ZElectron: 9.3.5Chrome: 83.0.4103.122Node.js: 12.14.1V8: 8.3.110.13-electron.0OS: Windows_NT x64 6.1.7601
复制代码 Node.js: 12.14.1 下载
3 npm设置淘宝镜像
npm设置为淘宝镜像:
- npm config set registry https://registry.npm.taobao.org
复制代码 检察是否设置乐成:
npm更新到最新版本:
- npm install -g npmornpm install -g npm@latest
复制代码 npm更新到指定版本:
4 npm安装TypeScript
npm全局安装TypeScript:
- npm install -g typescript
复制代码 检察TypeScript是否安装乐成:
5 VS Code调试TypeScript
新建TypeScript工程hello:
- let hello: string = "Hello World";console.log(hello)
复制代码 5.1 直接执行单个.ts文件
直接右键单击选中main.ts,执行Run Code即可在OUTPUT界面(快捷键:Ctrl+`打开OUTPUT界面)看到输出效果。
5.2 依赖生成.js文件调试
5.2.1 初始化npm项目
package.json形貌了npm项目依赖那些包,指明项目依赖的版本,让你的构建更好的与其他人共享。
生成package.json文件,在根目次执行:
- { "name": "hello", "version": "1.0.0", "description": "", "main": "main.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC"}
复制代码 参考链接:
5.2.2 设置tsconfig.json
如果一个目次下存在一个tsconfig.json文件,那么它意味着这个目次是TypeScript项目的根目次。tsconfig.json文件中指定了用来编译这个项目的根文件和编译选项。
在根目次自动生成tsconfig.json文件:
- { "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', 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": "./", /* 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 */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "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. */ // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ /* 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. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ /* 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. */ /* Advanced Options */ "skipLibCheck": true, /* Skip type checking of declaration files. */ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ }}
复制代码 调试需要设置"sourceMap": true,outDir设置js文件的生成目次。
5.2.3 设置自动编译(tasks.json)
菜单栏Terminal->Configure Tasks...分别选中"tsc: watch - tsconfig.json",则在工程根目次生成/.vscode/tasks.json,当ts文件生存后自动编译生成js文件:
- { "version": "2.0.0", "tasks": [ { "type": "typescript", "tsconfig": "tsconfig.json", "option": "watch", "problemMatcher": [ "$tsc-watch" ], "group": "build", "label": "tsc: watch - tsconfig.json" } ]}
复制代码 5.2.4 调试设置文件(launch.json)
菜单栏Run->Add Configuration...生成调试设置文件/.vscode/launch.json:
- { "version": "0.2.0", "configurations": [ { "type": "pwa-node", "request": "launch", "name": "Launch Program", "skipFiles": [ "/**" ], "program": "${workspaceFolder}\\main.ts", "outFiles": [ "${workspaceFolder}/**/*.js" ] } ]}
复制代码 此中"program": "${workspaceFolder}\\main.ts"是调试的ts文件生成的js文件。
别的,在launch.json增加preLaunchTask可以跳过5.2.3 设置自动编译:
- { "version": "0.2.0", "configurations": [ { "type": "pwa-node", "request": "launch", "name": "Launch Program", "skipFiles": [ "/**" ], "preLaunchTask": "tsc: build - tsconfig.json", "program": "${workspaceFolder}\\main.ts", "outFiles": [ "${workspaceFolder}/**/*.js" ] } ]}
复制代码 5.2.5 在.ts文件设置断点,开始调试
参考链接:
5.3 使用ts-node调试
5.3.1 安装ts-node
- # Locally in your project.npm install -D typescriptnpm install -D ts-node# Or globally with TypeScript.npm install -g typescriptnpm install -g ts-node
复制代码 5.3.2 设置tsconfig.json
同5.2.2
调试设置文件(launch.json)
菜单栏Run->Add Configuration...生成调试设置文件/.vscode/launch.json,添加-r ts-node/register到Node的执行参数(runtimeArgs) ,program添加到参数列表(args):
- { "version": "0.2.0", "configurations": [ { "type": "pwa-node", "request": "launch", "name": "Launch Program", "skipFiles": [ "/**" ], "runtimeArgs": [ "-r", "ts-node/register" ], "args": [ "${workspaceFolder}\\main.ts" ] } ]}
复制代码 注意: 当TypeScript,ts-node采取全局安装(npm install -g ts-node)时,ts-node/register应使用绝对路径:
- { "configurations": [ { ... "runtimeArgs": [ "-r", "C:/Users/wwchao/AppData/Roaming/npm/node_modules/ts-node/register" ], ... } ]}
复制代码 5.3.3 在ts文件设置断点,执行调试
同5.2.5 在ts文件设置断点,执行调试
参考链接:
6 Visual Studio Code:TypeScript官方教程
来源:https://blog.csdn.net/wwchao2012/article/details/112058729
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |