You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/blog/2024-12-04-differences-between-linting-and-type-checking.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,25 +37,7 @@ We'll explore the different ways the two use information from analyzing your cod
37
37
38
38
## Digging deeper into linting vs. type checking
39
39
40
-
Type checkers ensure that values are used only in ways that are allowed by the value's type. Compiled languages, like Java, perform type checking during the compilation phase. Because JavaScript has no way to indicate the intended type of a binding, it cannot perform type checking on its own. That's where TypeScript comes in.
41
-
42
-
By allowing explicit type annotations (and the implicit detection of some types), TypeScript overlays type information on top of JavaScript code to perform type checking similar to what's found in compiled languages.
43
-
For example, TypeScript reports a type error on the following `logUppercase(9001)` call, because `logUppercase` is declared to receive a `string` rather than a `number`:
44
-
45
-
```ts
46
-
function logUppercase(text:string) {
47
-
console.log(text.toUpperCase());
48
-
}
49
-
50
-
logUppercase(9001);
51
-
// ~~~~
52
-
// Argument of type 'number' is not assignable to parameter of type 'string'.
53
-
```
54
-
55
-
56
-
TypeScript focuses on reporting known errors rather than potential problems; there is nothing subjective about the errors that TypeScript reports, nor is there a way to implement project-specific preferences.
57
-
58
-
Linters, on the other hand, primarily report likely defects and can also be used to enforce subjective opinions.
40
+
Linters primarily report likely defects and can also be used to enforce subjective opinions.
59
41
ESLint and other linters catch issues that may or may not be type-safe but are potential sources of bugs.
60
42
Many developers rely on linters to make sure their code follows framework and language best practices.
Type checkers, on the other hand, ensure that values are used only in ways that are allowed by the value's type. Compiled languages, like Java, perform type checking during the compilation phase. Because JavaScript has no way to indicate the intended type of a binding, it cannot perform type checking on its own. That's where TypeScript comes in.
75
+
76
+
By allowing explicit type annotations (and the implicit detection of some types), TypeScript overlays type information on top of JavaScript code to perform type checking similar to what's found in compiled languages.
77
+
For example, TypeScript reports a type error on the following `logUppercase(9001)` call, because `logUppercase` is declared to receive a `string` rather than a `number`:
78
+
79
+
```ts
80
+
function logUppercase(text:string) {
81
+
console.log(text.toUpperCase());
82
+
}
83
+
84
+
logUppercase(9001);
85
+
// ~~~~
86
+
// Argument of type 'number' is not assignable to parameter of type 'string'.
87
+
```
88
+
89
+
TypeScript focuses on reporting known errors rather than potential problems; there is nothing subjective about the errors that TypeScript reports, nor is there a way to implement project-specific preferences.
90
+
92
91
Another way of looking at the differences between ESLint and TypeScript is that TypeScript enforces what you *can* do, whereas ESLint enforces what you *should* do.
93
92
94
93
### Granular Extensibility
95
94
96
95
Another difference between ESLint and TypeScript is in granularity of configuration.
97
96
98
-
TypeScript is configured by a set list of compiler options on a project level.
99
-
The [`tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) allows you to set compiler options that change type checking for all files in the project.
100
-
Those compiler options are set globally for TypeScript and generally change large swathes of type checking behavior.
101
-
102
-
ESLint, on the other hand, runs with a set of individually configurable lint rules. If you don't like a particular lint rule, you can turn it off for a line, set of files, or your entire project.
97
+
ESLint runs with a set of individually configurable lint rules. If you don't like a particular lint rule, you can turn it off for a line, set of files, or your entire project.
103
98
ESLint can also be augmented by **plugins** that add new lint rules.
104
99
Plugin-specific lint rules extend the breadth of code checks that ESLint configurations can pick and choose from.
By adding in rules from plugins, ESLint configurations can be tailored to the specific best practices and common issues to the frameworks a project is built with.
130
125
126
+
TypeScript, on the other hand, is configured by a set list of compiler options on a project level.
127
+
The [`tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) allows you to set compiler options that change type checking for all files in the project.
128
+
Those compiler options are set globally for TypeScript and generally change large swathes of type checking behavior.
129
+
TypeScript does not allow compiler options to be different across different files in a single project.
130
+
131
131
### Areas of Overlap
132
132
133
133
While ESLint and TypeScript operate differently and specialize in different areas of code defects, there is some overlap. Specific types of code defects straddle the line between "best practices" and "type safety," and so can be caught by both tools.
0 commit comments