Skip to content

Commit b734004

Browse files
committed
new errors section
1 parent d267771 commit b734004

File tree

3 files changed

+55
-56
lines changed

3 files changed

+55
-56
lines changed

SUMMARY.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
* [Options](docs/options/intro.md)
6868
* [noImplicitAny](docs/options/noImplicitAny.md)
6969
* [strictNullChecks](docs/options/strictNullChecks.md)
70+
* [Errors in TypeScript](docs/errors/main.md)
71+
* [Reading Errors](docs/errors/reading-errors.md)
72+
* [Common Errors](docs/errors/common-errors.md)
7073
* [Testing](docs/testing/intro.md)
7174
* [Jest](docs/testing/jest.md)
7275
* [Cypress](docs/testing/cypress.md)
@@ -96,7 +99,6 @@
9699
* [Create Arrays](docs/tips/create-arrays.md)
97100
* [Typesafe Event Emitter](docs/tips/typed-event.md)
98101
* [StyleGuide](docs/styleguide/styleguide.md)
99-
* [Common Errors](docs/errors/main.md)
100102
* [TypeScript Compiler Internals](docs/compiler/overview.md)
101103
* [Program](docs/compiler/program.md)
102104
* [AST](docs/compiler/ast.md)

docs/errors/common-errors.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Common Errors
2+
In this section we explain a number of common error codes that users experience in the real world.
3+
4+
## TS2304
5+
Samples:
6+
> `Cannot find name ga`
7+
> `Cannot find name $`
8+
> `Cannot find module jquery`
9+
10+
You are probably using a third party library (e.g. google analytics) and don't have it `declare`d. TypeScript tries to save you from *spelling mistakes* and *using variables without declaring them* so you need to be explicit on anything that is *available at runtime* because of you including some external library ([more on how to fix it][ambient]).
11+
12+
## TS2307
13+
Samples:
14+
> `Cannot find module 'underscore'`
15+
16+
You are probably using a third party library (e.g. underscore) as a *module* ([more on modules][modules]) and don't have the ambient declaration file for it ([more on ambient declarations][ambient]).
17+
18+
## TS1148
19+
Sample:
20+
> Cannot compile modules unless the '--module' flag is provided
21+
22+
Checkout the [section on modules][modules].
23+
24+
## Catch clause variable cannot have a type annotation
25+
Sample:
26+
```js
27+
try { something(); }
28+
catch (e: Error) { // Catch clause variable cannot have a type annotation
29+
}
30+
```
31+
TypeScript is protecting you from JavaScript code in the wild being wrong. Use a type guard instead:
32+
```js
33+
try { something(); }
34+
catch (e) {
35+
if (e instanceof Error){
36+
// Here you go.
37+
}
38+
}
39+
```
40+
41+
## Interface `ElementClass` cannot simultaneously extend types `Component` and `Component`
42+
This happens when you have two `react.d.ts` (`@types/react/index.d.ts`) in the compilation context.
43+
44+
**Fix**:
45+
* Delete `node_modules` and any `package-lock` (or yarn lock) and `npm install` again.
46+
* If it doesn't work, find the invalid module (all modules used by your project should have `react.d.ts` as a `peerDependency` and not a hard `dependency`) and report it on their project.
47+
48+
49+
[ambient]: ../types/ambient/d.ts.md
50+
[modules]: ../project/modules.md

docs/errors/main.md

+2-55
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,2 @@
1-
# Common Errors
2-
In this section we explain a number of common error codes that users experience in the real world.
3-
4-
## TS2304
5-
Samples:
6-
> `Cannot find name ga`
7-
8-
You are probably using a third party library (e.g. google analytics) and don't have it `declare`d. TypeScript tries to save you from *spelling mistakes* and *using variables without declaring them* so you need to be explicit on anything that is *available at runtime* because of you including some external library ([more on how to fix it][ambient]).
9-
10-
## TS2307
11-
Samples:
12-
> `Cannot find module 'underscore'`
13-
14-
You are probably using a third party library (e.g. underscore) as a *module* ([more on modules][modules]) and don't have the ambient declaration file for it ([more on ambient declarations][ambient]).
15-
16-
## TS1148
17-
Sample:
18-
> Cannot compile modules unless the '--module' flag is provided
19-
20-
Checkout the [section on modules][modules].
21-
22-
## Catch clause variable cannot have a type annotation
23-
Sample:
24-
```js
25-
try { something(); }
26-
catch (e: Error) { // Catch clause variable cannot have a type annotation
27-
}
28-
```
29-
TypeScript is protecting you from JavaScript code in the wild being wrong. Use a type guard instead:
30-
```js
31-
try { something(); }
32-
catch (e) {
33-
if (e instanceof Error){
34-
// Here you go.
35-
}
36-
}
37-
```
38-
39-
## Interface `ElementClass` cannot simultaneously extend types `Component` and `Component`
40-
This happens when you have two `react.d.ts` (`@types/react/index.d.ts`) in the compilation context.
41-
42-
**Fix**:
43-
* Delete `node_modules` and any `package-lock` (or yarn lock) and `npm install` again.
44-
* If it doesn't work, find the invalid module (all modules used by your project should have `react.d.ts` as a `peerDependency` and not a hard `dependency`) and report it on their project.
45-
46-
47-
## For search indexing
48-
You can ignore reading this. This section is for search engine indexing.
49-
50-
> Other modules that people tend to use and get errors:
51-
* Cannot find name $
52-
* Cannot find module jquery
53-
54-
[ambient]: ../types/ambient/d.ts.md
55-
[modules]: ../project/modules.md
1+
# Errors
2+
In this section we discuss how to read and understand TypeScript errors. We follow this with common errors and their solutions.

0 commit comments

Comments
 (0)