Skip to content

Commit 0041bc2

Browse files
authored
Update TypeScript guide (#572)
1 parent 1154ee4 commit 0041bc2

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

docs/reference/using_typescript.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
11
# Using Typescript
22

3-
Stimulus itself is written [TypeScript](https://www.typescriptlang.org/)
4-
and provides types directly over its package.
5-
The following documentation should make it clear how to define types for
6-
stimulus properties.
3+
Stimulus itself is written in [TypeScript](https://www.typescriptlang.org/) and provides types directly over its package.
4+
The following documentation shows how to define types for Stimulus properties.
75

86
## Define Controller Element Type
97

10-
By default, the `element` of the controller is from type `Element`.
11-
If the element expected type is as example `HTMLFormElement` it can
12-
be defined this way:
8+
By default, the `element` of the controller is of type `Element`. You can override the type of the controller element by specifiying it as a [Generic Type](https://www.typescriptlang.org/docs/handbook/2/generics.html). For example, if the element type is expected to be a `HTMLFormElement`:
9+
10+
<meta data-controller="callout" data-callout-text-value="Controller<HTMLFormElement>">
1311

1412
```ts
15-
import { Controller } from '@hotwired/stimulus';
13+
import { Controller } from "@hotwired/stimulus"
1614

1715
export default class MyController extends Controller<HTMLFormElement> {
18-
submit() {
19-
new FormData(this.element)
20-
}
16+
submit() {
17+
new FormData(this.element)
18+
}
2119
}
2220
```
2321

24-
## Define Controller Value Type
22+
## Define Value Properties
2523

26-
To define the type of configured values is possible
27-
over the declare feature of TypeScript:
24+
You can define the properties of configured values using the TypeScript `declare` keyword. You just need to define the properties if you are making use of them within the controller.
2825

2926
```ts
30-
import { Controller } from '@hotwired/stimulus';
27+
import { Controller } from "@hotwired/stimulus"
3128

3229
export default class MyController extends Controller {
33-
static values = {
34-
code: String,
35-
};
30+
static values = {
31+
code: String
32+
}
3633

37-
declare readonly codeValue: string;
34+
declare codeValue: string
35+
declare readonly hasCodeValue: boolean
3836
}
3937
```
4038

41-
> The `declare` avoids override of the exist stimulus property and just define the type for TypeScript.
39+
> The `declare` keyword avoids overriding the existing Stimulus property, and just defines the type for TypeScript.
40+
41+
## Define Target Properties
4242

43-
## Define Controller Target Type
43+
You can define the properties of configured targets using the TypeScript `declare` keyword. You just need to define the properties if you are making use of them within the controller.
4444

45-
To define the type of configured targets is possible
46-
over the declare feature of TypeScript:
45+
The return types of the `[name]Target` and `[name]Targets` properties can be any inheriting from the `Element` type. Choose the best type which fits your needs. Pick either `Element` or `HTMLElement` if you want to define it as a generic HTML element.
4746

4847
```ts
49-
import { Controller } from '@hotwired/stimulus';
48+
import { Controller } from "@hotwired/stimulus"
5049

5150
export default class MyController extends Controller {
52-
static targets = [
53-
'input'
54-
];
51+
static targets = [ "input" ]
5552

56-
declare readonly inputTarget: HTMLInputElement;
53+
declare readonly hasInputTarget: boolean
54+
declare readonly inputTarget: HTMLInputElement
55+
declare readonly inputTargets: HTMLInputElement[]
5756
}
5857
```
5958

60-
> The `declare` avoids override of the exist stimulus property and just define the type for TypeScript.
59+
> The `declare` keyword avoids overriding the existing Stimulus property, and just defines the type for TypeScript.
60+
61+
## Custom properties and methods
6162

62-
## Custom properties amd methods
63+
Other custom properties can be defined the TypeScript way on the controller class:
6364

64-
Other custom properties is possible to define the TypeScript way
65-
on the controller class:
65+
<meta data-controller="callout" data-callout-text-value="container: HTMLElement">
6666

6767
```ts
68-
import { Controller } from '@hotwired/stimulus';
68+
import { Controller } from "@hotwired/stimulus"
6969

7070
export default class MyController extends Controller {
71-
container: HTMLElement;
71+
container: HTMLElement
7272
}
7373
```
7474

75-
Read more about it in the [TypeScript Documentation](https://www.typescriptlang.org/docs/handbook/intro.html).
75+
Read more in the [TypeScript Documentation](https://www.typescriptlang.org/docs/handbook/intro.html).

0 commit comments

Comments
 (0)