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: docs/_guide/decorators.md
+25-21Lines changed: 25 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,27 +37,31 @@ class HelloWorldElement extends HTMLElement {
37
37
38
38
Class Field decorators get given the class and the field name so they can add custom functionality to the field. Because they operate on the fields, they must be put on top of or to the left of the field.
39
39
40
-
#### Disabling `strictPropertyInitialization`
41
-
42
-
TypeScript comes with various "strict" mode settings, one of which is `strictPropertyInitialization` which TypeScript catch potential class properties which might not be assigned during construction of a class. This option conflicts with Catalyst's `@target`/`@targets` decorators, which safely do the assignment but TypeScript's simple heuristics cannot detect this. It's recommended to disable this option (other strict mode rules can still apply) in your `tsconfig.json` like so:
43
-
44
-
```json
45
-
{
46
-
"compilerOptions": {
47
-
"strict": true,
48
-
"strictPropertyInitialization": false
49
-
}
50
-
}
51
-
```
52
-
53
-
If you really want to keep the `strictPropertyInitialization` option set to `true`, another option would be to use TypeScript's [non-null assertion operator (`!`)](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator), adding this to each of your `@target`/`@targets` properties, like so:
54
-
55
-
```typescript
56
-
classHelloWorldElementextendsHTMLElement {
57
-
@target something!:HTMLElement
58
-
@targets items!:HTMLElement[]
59
-
}
60
-
```
40
+
#### Supporting `strictPropertyInitialization`
41
+
42
+
TypeScript comes with various "strict" mode settings, one of which is `strictPropertyInitialization` which lets TypeScript catch potential class properties which might not be assigned during construction of a class. This option conflicts with Catalyst's `@target`/`@targets` decorators, which safely do the assignment but TypeScript's simple heuristics cannot detect this. There are two ways to work around this:
43
+
44
+
1. Use TypeScript's [`declare` modifier](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier) to tell TypeScript that the decorated field will still be set up correctly:
45
+
46
+
```typescript
47
+
classHelloWorldElementextendsHTMLElement {
48
+
@targetdeclare something:HTMLElement
49
+
@targetsdeclare items:HTMLElement[]
50
+
}
51
+
```
52
+
53
+
NotethatthisonlyworksonTypeScript3.7+, soifyou're on an older version, you can also use the [definite initialization operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#definite-assignment-assertions) to do the same thing.
0 commit comments