简体   繁体   English

TypeScript 和 typescript-eslint 有重叠的用途吗?

[英]Do TypeScript and typescript-eslint have overlapping purposes?

I was reading the typescript-eslint docs and I came into this section:我正在阅读typescript-eslint文档,然后进入了这一部分:

TypeScript and ESLint have similar purposes TypeScript 和 ESLint 有相似的用途
This means that there will be cases where TypeScript actually solves a problem for us that we previously relied on ESLint for.这意味着在某些情况下,TypeScript 实际上为我们解决了以前依赖 ESLint 的问题。

Having their definition in mind, one is a statically typed language and one is a tool that parses code and puts some assertions in order to make sure some rules & patterns are considered, which are totally different things.考虑到它们的定义,一种是静态类型语言,一种是解析代码并放置一些断言以确保考虑某些规则和模式的工具,这是完全不同的东西。 I wanted to know what examples can be given for the part solves a problem for us that we previously relied on ESLint for .我想知道可以给出哪些示例来solves a problem for us that we previously relied on ESLint for Do these two things have anything in common?这两件事有什么共同点吗? What are their similar purposes?它们的相似目的是什么?

Because JavaScript is a dynamically typed language, programmers can more easily introduce subtle bugs and errors that fail at runtime compared to static languages with a compiler that picks up on common issues.由于 JavaScript 是一种动态类型语言,因此与使用编译器处理常见问题的静态语言相比,程序员可以更轻松地引入细微的错误和错误,这些错误和错误在运行时会失败。

Take the following code for example:以下面的代码为例:

console.log(someUndefinedVariable)

const constant = 0
constant = 3

const addOne = n => {
  if (typeof n !== 'nubmer') {
    throw new Error('n must be an number')
    console.log('This will never be executed')
  }
  if (n < 0) console.log('negative number')
  else return n + 1
}
console.log(addOne(-3) * 2)

const symbol = new Symbol()

This code will fail at runtime and also has some other issues that may lead to unexpected results.此代码将在运行时失败,并且还有一些其他可能导致意外结果的问题。

Linters such as ESLint pick up on some of these issues with rules such as no-undef and no const-assign:诸如 ESLint 之类的 Linter 通过诸如 no-undef 和 no const-assign 之类的规则来解决其中的一些问题

 1:13  error  'someUndefinedVariable' is not defined      no-undef
 4:1   error  'constant' is constant                      no-const-assign
 7:20  error  Invalid typeof comparison value             valid-typeof
 9:3   error  Unreachable code                            no-unreachable
16:20  error  `Symbol` cannot be called as a constructor  no-new-symbol

Similarly, TypeScript's compiler will also warn you about many of these issues :同样,TypeScript 的编译器也会就以下许多问题向您发出警告

Cannot find name 'someUndefinedVariable'.

Cannot assign to 'constant' because it is a constant.

This condition will always return 'true' since the types
'"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"'
and '"nubmer"' have no overlap.

Unreachable code detected.

'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

In this sense, ESLint and TypeScript have the same goal: to prevent likely programmer errors by warning you beforehand.从这个意义上说,ESLint 和 TypeScript 有相同的目标:通过事先警告你来防止可能的程序员错误。 For these issues, you can turn off the respective ESLint rules and use the TypeScript compiler instead.对于这些问题,您可以关闭相应的 ESLint 规则并改用 TypeScript 编译器。


However, the most important feature about TypeScript is its addition of static types to JavaScript.然而,TypeScript 最重要的特性是它向 JavaScript 添加了静态类型。 By adding type annotations to addOne :通过向addOne添加类型注释:

const addOne = (n: number): number => { /* ... */ }

TS tells us that Function lacks ending return statement and return type does not include 'undefined'. TS 告诉我们, Function lacks ending return statement and return type does not include 'undefined'. because the function will return undefined instead of a number if n is a negative number.因为如果n是负数,函数将返回undefined而不是数字。 The result of addOne(-3) * 2 would be NaN , instead of -4 like expected. addOne(-3) * 2将是NaN ,而不是像预期的-4

Another example, which ESLint would be completely fine with but fails at runtime:另一个例子,ESLint 完全可以使用但在运行时失败:

const foo = 0
const bar = foo()
//          ~~~
// This expression is not callable.
//   Type 'Number' has no call signatures.

These are some of the many issues that TypeScript can help identify due to its type system.由于 TypeScript 的类型系统,这些是 TypeScript 可以帮助识别的许多问题中的一些。

On the other hand, linters including ESLint and the typescript-eslint plugin can enforce best practices such as using strict equality operators and correctly handling promises .另一方面,包括 ESLint 和 typescript-eslint 插件在内的 linter 可以强制执行最佳实践,例如使用严格相等运算符正确处理 promises They can also enforce stylistic conventions such as indentation , requiring or forbidding semicolons , or consistent type assertion styles .它们还可以强制执行样式约定,例如缩进要求或禁止分号一致的类型断言样式


TypeScript and ESLint have the similar goal of preventing programmer bugs and errors. TypeScript 和 ESLint 具有防止程序员错误和错误的相似目标。 However, due to its type system, TypeScript can pick up on more runtime and programming errors , whereas ESLint can enforce stylistic conventions and best practices .然而,由于它的类型系统,TypeScript 可以发现更多的运行时和编程错误,而 ESLint 可以强制执行风格约定最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM