|
1 | 1 | # Using Typescript |
2 | 2 |
|
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. |
7 | 5 |
|
8 | 6 | ## Define Controller Element Type |
9 | 7 |
|
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>"> |
13 | 11 |
|
14 | 12 | ```ts |
15 | | -import { Controller } from '@hotwired/stimulus'; |
| 13 | +import { Controller } from "@hotwired/stimulus" |
16 | 14 |
|
17 | 15 | export default class MyController extends Controller<HTMLFormElement> { |
18 | | - submit() { |
19 | | - new FormData(this.element) |
20 | | - } |
| 16 | + submit() { |
| 17 | + new FormData(this.element) |
| 18 | + } |
21 | 19 | } |
22 | 20 | ``` |
23 | 21 |
|
24 | | -## Define Controller Value Type |
| 22 | +## Define Value Properties |
25 | 23 |
|
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. |
28 | 25 |
|
29 | 26 | ```ts |
30 | | -import { Controller } from '@hotwired/stimulus'; |
| 27 | +import { Controller } from "@hotwired/stimulus" |
31 | 28 |
|
32 | 29 | export default class MyController extends Controller { |
33 | | - static values = { |
34 | | - code: String, |
35 | | - }; |
| 30 | + static values = { |
| 31 | + code: String |
| 32 | + } |
36 | 33 |
|
37 | | - declare readonly codeValue: string; |
| 34 | + declare codeValue: string |
| 35 | + declare readonly hasCodeValue: boolean |
38 | 36 | } |
39 | 37 | ``` |
40 | 38 |
|
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 |
42 | 42 |
|
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. |
44 | 44 |
|
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. |
47 | 46 |
|
48 | 47 | ```ts |
49 | | -import { Controller } from '@hotwired/stimulus'; |
| 48 | +import { Controller } from "@hotwired/stimulus" |
50 | 49 |
|
51 | 50 | export default class MyController extends Controller { |
52 | | - static targets = [ |
53 | | - 'input' |
54 | | - ]; |
| 51 | + static targets = [ "input" ] |
55 | 52 |
|
56 | | - declare readonly inputTarget: HTMLInputElement; |
| 53 | + declare readonly hasInputTarget: boolean |
| 54 | + declare readonly inputTarget: HTMLInputElement |
| 55 | + declare readonly inputTargets: HTMLInputElement[] |
57 | 56 | } |
58 | 57 | ``` |
59 | 58 |
|
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 |
61 | 62 |
|
62 | | -## Custom properties amd methods |
| 63 | +Other custom properties can be defined the TypeScript way on the controller class: |
63 | 64 |
|
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"> |
66 | 66 |
|
67 | 67 | ```ts |
68 | | -import { Controller } from '@hotwired/stimulus'; |
| 68 | +import { Controller } from "@hotwired/stimulus" |
69 | 69 |
|
70 | 70 | export default class MyController extends Controller { |
71 | | - container: HTMLElement; |
| 71 | + container: HTMLElement |
72 | 72 | } |
73 | 73 | ``` |
74 | 74 |
|
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