Skip to content

Commit b21d40b

Browse files
authored
Merge pull request #55 from github/docs-remove-ts-s-add-docs-note-about-them
docs: Remove TS !s, add docs note about them.
2 parents 2885cc2 + 04fc9e7 commit b21d40b

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

docs/_guide/anti-patterns.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class UserListElement extends HTMLElement {
258258

259259
```typescript
260260
class UserList {
261-
@targets admins!: HTMLElement[]
261+
@targets admins: HTMLElement[]
262262

263263
showAdmins() {
264264
// Just need to get admins here...
@@ -282,7 +282,7 @@ For example let's say we have a list of filter checkboxes and checking the "all"
282282
```typescript
283283
@controller
284284
class UserFilter {
285-
@targets filters!: HTMLInputElement[]
285+
@targets filters: HTMLInputElement[]
286286

287287
get allFilter() {
288288
return this.filters.find(el => el.matches('[data-filter="all"]'))
@@ -325,8 +325,8 @@ While this works well, it could be more easily solved with targets:
325325
```typescript
326326
@controller
327327
class UserFilter {
328-
@targets filters!: HTMLInputElement[]
329-
@target allFilter!: HTMLInputElement
328+
@targets filters: HTMLInputElement[]
329+
@target allFilter: HTMLInputElement
330330

331331
filter(event: Event) {
332332
if (event.target === this.allFilter) {

docs/_guide/decorators.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ class HelloWorldElement extends HTMLElement {
3737

3838
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.
3939

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+
class HelloWorldElement extends HTMLElement {
57+
@target something!: HTMLElement
58+
@targets items!: HTMLElement[]
59+
}
60+
```
61+
4062
### Method Decorators
4163

4264
Catalyst doesn't currently ship with any method decorators, but you might see them in code. They work just like Field Decorators (in fact they're the same thing). Put them on top or on the left of the method, like so:

docs/_guide/targets.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { controller, target } from "@github/catalyst"
3030

3131
@controller
3232
class HelloWorldElement extends HTMLElement {
33-
@target outputTarget!: HTMLElement
33+
@target outputTarget: HTMLElement
3434

3535
greet() {
3636
this.outputTarget.textContent = `Hello, world!`
@@ -92,8 +92,8 @@ import { controller, target, targets } from "@github/catalyst"
9292

9393
@controller
9494
class UserSettingsElement extends HTMLElement {
95-
@target read!: HTMLInputElement
96-
@target write!: HTMLInputElement
95+
@target read: HTMLInputElement
96+
@target write: HTMLInputElement
9797

9898
valid() {
9999
// One checkbox must be checked!
@@ -103,7 +103,7 @@ class UserSettingsElement extends HTMLElement {
103103

104104
@controller
105105
class UserListElement extends HTMLElement {
106-
@targets user!: HTMLElement
106+
@targets user: HTMLElement
107107

108108
valid() {
109109
// Every user must be valid!

docs/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ <h1 class="h000-mktg mt-6 mr-6">Catalyse your Web Components</h1>
4646

4747
@controller
4848
class HelloWorldElement extends HTMLElement {
49-
@target name!: HTMLElement
50-
@target output!: HTMLElement
49+
@target name: HTMLElement
50+
@target output: HTMLElement
5151

5252
greet() {
5353
this.output.textContent = `Hello, ${this.name.value}!`

0 commit comments

Comments
 (0)