# 设置

  1. 启用括号高亮:editor.bracketPairColorization.enabled:true,不再需要Bracket Pair Colorizer 2插件

# 命令

# 指定文件扩展名为特定语言

F1>change language mode

# snippets代码段

  1. 如果想要智能提示中代码段排在最上面,需要设置"editor.snippetSuggestions": "top"
  2. 输入特殊字符时需要使用\\转义
  3. 换行\n
  4. tab键制表符:\t
  5. 如果想光标选中字符则${1:test}

# git

# autostash

设置git pull之前先stash,pull之后再pop掉。开启方法:设置>扩展>git>勾选Auto Stash

注意,需求git版本在2.9以上,在内置终端执行git pull是无效的,只能通过vscode的命令才能生效

# 代码段变量

Variables (opens new window)

# 命令行工具

# 用vscode新窗口打开当前文件夹

code .

# 用当前vscode窗口打开文件夹

code . -r

# 配置假名路径的智能提示和跳转

jsconfig.json文件放到根目录下,记得添加到.gitignore

// jsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "target": "ES6",
    "module": "commonjs",
    "allowSyntheticDefaultImports": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

# 类型提示

# vscode引入类型提示的几种方式

  1. vscode程序自带npm包Visual Studio Code.app>Contents>Resources>app>extensions>node_modules>typescript>lib包含一些
  2. npm包通过设置types或者typings,来告知vscode类型声明文件d.ts的位置,这样就可以提供类型提示了。
  3. 通过把类型文件与引入文件放在同一个路径中,并且文件名相同时,vscode也会添加类型提示。
  4. 安装@types/*格式的包,则会自动加载类型提示

# js项目生成d.ts文件

参考:https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html

package.json添加typescript,然后添加tsconfig

{
  // Change this to match your project
  "include": ["src/**/*"],
  "compilerOptions": {
    // Tells TypeScript to read JS files, as
    // normally they are ignored as source files
    "allowJs": true,
    // Generate d.ts files
    "declaration": true,
    // This compiler run should
    // only output d.ts files
    "emitDeclarationOnly": true,
    // Types should go into this directory.
    // Removing this would place the .d.ts files
    // next to the .js files
    "outDir": "dist",
    // go to js file when using IDE functions like
    // "Go to Definition" in VSCode
    "declarationMap": true
  }
}

运行命令 tsc进行编译

# 类型文件加载顺序

  1. types
  2. main
  3. ./index.d.ts

# 类型检查

vscode中可以通过添加// @ts-check注释来开启类型检查,除了这个方法,还可以通过配置全局开启,只需要勾选Implicit Project Config: Check JS,jsoncofng.jsontsconfig.json中的配置会替代此配置