Skip to content

Commit 2036d3c

Browse files
Ordering: lint, then type check
1 parent 6324cce commit 2036d3c

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/content/blog/2024-12-04-differences-between-linting-and-type-checking.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,7 @@ We'll explore the different ways the two use information from analyzing your cod
3737

3838
## Digging deeper into linting vs. type checking
3939

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.
5941
ESLint and other linters catch issues that may or may not be type-safe but are potential sources of bugs.
6042
Many developers rely on linters to make sure their code follows framework and language best practices.
6143

@@ -89,17 +71,30 @@ function logFruit(value: "apple" | "banana" | "cherry") {
8971
logFruit("banana");
9072
```
9173

74+
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+
9291
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.
9392

9493
### Granular Extensibility
9594

9695
Another difference between ESLint and TypeScript is in granularity of configuration.
9796

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.
10398
ESLint can also be augmented by **plugins** that add new lint rules.
10499
Plugin-specific lint rules extend the breadth of code checks that ESLint configurations can pick and choose from.
105100

@@ -128,6 +123,11 @@ const MyComponent = () => <img src="source.webp" />;
128123

129124
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.
130125

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+
131131
### Areas of Overlap
132132

133133
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

Comments
 (0)