在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ Typescript 代碼類型檢查不通過時(shí),可以用 TSlint 監(jiān)測(cè)到么?

Typescript 代碼類型檢查不通過時(shí),可以用 TSlint 監(jiān)測(cè)到么?

問題描述

  • 情況1:如果是屬性不存在, IDE 會(huì)提醒錯(cuò)誤,但是 TSlint/ESlint 不會(huì)拋出錯(cuò)誤
    clipboard.png
  • 情況2:TSlint 只會(huì)拋出在 rules 中有定義的錯(cuò)誤,例如下面限制了no-empty-interfacerules, 就會(huì)拋出錯(cuò)誤
    clipboard.png

相關(guān)代碼

  • TSlint.json 文件

    {
      "defaultSeverity": "warning",
      "extends": [
        "tslint:recommended"
      ],
      "linterOptions": {
        "exclude": [
          "node_modules/**"
        ]
      },
      "rules": {
        "quotemark": [true, "single"],
        "indent": [true, "spaces", 2],
        "interface-name": false,
        "ordered-imports": false,
        "object-literal-sort-keys": false,
        "no-consecutive-blank-lines": false
      }
    }
    
  • tsconfig.json

    {
      "compilerOptions": {
        "target": "esnext",
        "module": "esnext",
        "strict": true,
        "jsx": "preserve",
        "importHelpers": true,
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "baseUrl": ".",
        "types": [
          "node"
        ],
        "paths": {
          "@/*": [
            "src/*"
          ]
        },
        "lib": [
          "esnext",
          "dom",
          "dom.iterable",
          "scripthost"
        ]
      },
      "include": [
        "src/**/*.ts",
        "src/**/*.tsx",
        "src/**/*.vue",
        "tests/**/*.ts",
        "tests/**/*.tsx"
      ],
      "exclude": [
        "node_modules"
      ]
    }
    

你期待的結(jié)果是什么?

TSlint 能夠檢查到并拋出 TSlint 代碼類型檢查(情況1)的錯(cuò)誤么?
如果能,如何設(shè)置?
如果不能,有什么方法可以在gitHooks.pre-commit 攔截(情況1)的錯(cuò)誤,未解決錯(cuò)誤禁止提交代碼么?

回答
編輯回答
故人嘆

我沒有研究過TSlint,但是從軟件開發(fā)的角度來講,TSlint不應(yīng)該拋出類型檢查錯(cuò)誤。

  • lint類工具設(shè)計(jì)就是用來檢查代碼格式(美觀度)的,而不是檢查邏輯的
  • 如果TSlint自己做代碼檢查,就要重造tsc的輪子
  • 如果TSlint調(diào)用tsc,還不如讓用戶自己去調(diào)用

如果要加上類型檢查,首先需要安裝typescript

npm install typescript

然后在pre-commit里加上調(diào)用tsc的代碼。取決于是不是全局安裝,代碼會(huì)略有差異。

TSC="$(git rev-parse --show-toplevel)/node_modules/.bin/tsc"
"$TSC" --noEmit
if [[ "$?" != 0 ]]; then
  printf "\033[41mERR: type check failed\033[0m"
  exit 1
fi
2017年7月14日 23:50