如何处理Typescript编译问题? [英] How to lint for Typescript compilation issues?

查看:454
本文介绍了如何处理Typescript编译问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用以下Typescript箭头功能:

Take the following Typescript arrow function:

/**
 * Returns a probably unique component name.
 * 
 * @param baseName a suggested name to make unique.
 * @returns a probably unique name.
 */
export const getUniqueComponentName = (
  baseName
): string => {
  return baseName + Math.round(Math.random() * 10000000)
}

当在tsconfig.json中配置了Typescript时:

When Typescript is configured in tsconfig.json as such:

"noImplicitAny": true,

这正确地导致了编译错误:

This correctly results in a compilation error:

[ts]参数'baseName'隐式具有'any'类型.

[ts] Parameter 'baseName' implicitly has an 'any' type.

Visual Studio Code也足够聪明,可以在开发过程中通知您有关此问题的信息.

Visual Studio Code is also smart enough to inform you about this issue during development.

我的目标是创建一个预提交git钩子,以防止此类错误最终出现在版本控制中.我尝试使用npm scripttslinthuskylint-staged来做到这一点:

My goal is to create a precommit git hook that prevents such errors from ending up in version control. I tried to do this with tslint, husky and lint-staged using this npm script:

"lint": "tslint --project tsconfig.json --config tslint.json"

但是,这不会导致tslint出现编译错误.它被默默忽略.

However, this does not result in the compilation error showing up by tslint. It is silently ignored.

然后我尝试在tslint.json中添加一条规则:

I then tried to add a rule in tslint.json:

"typedef": [
      true,
      "arrow-parameter"
    ]

虽然这确实使tslint抱怨,但它也开始在tsc编译器没有抱怨的匿名箭头函数中抱怨.在这些箭头函数中,不必添加类型,因为这些类型已经在父作用域中预先设置(可以推断出来).

While this did make tslint complain, it also started to complain in anonymous arrow functions where the tsc compiler does not complain. In these arrow functions it should not be necessary to add types because the types were already set previously in the parent scope (they are inferred).

因此,基本上,我希望tslint在这种情况下的行为与tsc相同.每当有错误会导致编译失败(例如上述箭头功能)时,我都想阻止提交,但实际上并没有编译为Javascript.这可能吗?

So basically, I would like for tslint to behave the same as tsc in this case. Anytime there is an error that would cause compilation to fail (such as the above arrow function), I would like to prevent the commit, but without actually compiling to Javascript. Is this possible?

推荐答案

我认为您最好的选择是运行tsc --noEmit -p .并为修改后的文件中的错误过滤输出.例如,我将以下脚本保存到tsc-some-files:

I think your best bet is to run tsc --noEmit -p . and filter the output for errors in the modified files. For example, I saved the following script to tsc-some-files:

#!/bin/bash
declare -A include_files
for f in "$@"; do
  include_files["${f#$PWD/}"]=1
done
node_modules/.bin/tsc --noEmit -p . | (
  status=0
  show_continuation=false
  while IFS='' read -r line; do
    case "$line" in
    (' '*)
      if $show_continuation; then
        echo "$line" >&2
      fi
      ;;
    (*)
      file="${line%%(*}"
      if [ -n "${include_files["$file"]}" ]; then
        show_continuation=true
        echo "$line" >&2
        status=1
      else
        show_continuation=false
      fi
      ;;
    esac
  done
  exit $status
)

并将./tsc-some-files设置为我的lint-staged命令,它似乎可以正常工作. (如果需要,可以用除bash之外的其他编程语言来编写代码,作为练习供读者使用.)

and set ./tsc-some-files as my lint-staged command, and it seemed to work. (Writing this in a programming language other than bash, if desired, is left as an exercise for the reader.)

请记住,尽管编辑一个文件会在另一个文件中引入错误(例如,如果您更改了另一个文件正在使用的内容的类型),那么我强烈建议您清除项目中的TypeScript错误尽快采取必要的措施(只要您标记了它们,以便以后可以搜索它们),然后设置钩子,以使整个项目中没有错误.实际上,特别是对于noImplicitAny,几年前当我将JavaScript项目迁移到TypeScript时,我编写了一个脚本,该脚本在有隐式any错误的任何地方插入了显式的any,然后我修复了显式的any在我闲暇时.如果您有兴趣,我可以分享该脚本.

Keep in mind though that editing one file can introduce an error in another file (e.g., if you changed the type of something that the other file is using), so I'd urge you to get your project clean of TypeScript errors ASAP by whatever hacks necessary (as long as you mark them so you can search for them later) and then set your hook to require no errors in the whole project. In fact, with respect to noImplicitAny in particular, when I migrated a JavaScript project to TypeScript several years ago, I wrote a script that inserted an explicit any everywhere there was an implicit any error, then I fixed the explicit anys at my leisure. I can share the script if you're interested.

这篇关于如何处理Typescript编译问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆