Skip to content

Commit a2bac52

Browse files
authored
Merge pull request #256 from github/tweaks-to-create-ability-docs
tweaks to create-ability docs
2 parents bda0af8 + ddd1dd7 commit a2bac52

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

docs/_guide/create-ability.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This function allows you to make your own [Ability]({{ site.baseurl }}/guide/abi
2121

2222
The above three features of `createAbility` make it really useful when creating mixins for web components, and makes them much easier for developers as they can trust an ability to not be sensitive to these problems.
2323

24-
To create an ability, call the `createAbility` method and pass in a callback function which takes a `CustomElementClass` and returns a new class. You can also provide extra types if your returned class requires them. Here's an example, using TypeScript:
24+
To create an ability, call the `createAbility` method and pass in a callback function which takes a `CustomElementClass` and returns a new class. You can also provide extra types if your returned class adds new methods or fields. Here's an example, using TypeScript:
2525

2626

2727
```typescript
@@ -35,6 +35,7 @@ interface Fooable {
3535

3636
// Fooable: automatically calls `foo()` on `connectedCallback`
3737
export const fooable = createAbility(
38+
// ↓ Notice the `& { new (): Fooable }`
3839
<T extends CustomElementClass>(Class: T): T & { new (): Fooable } =>
3940
class extends Class {
4041
connectedCallback() {
@@ -45,6 +46,7 @@ export const fooable = createAbility(
4546
console.log('Foo was called!')
4647
}
4748
}
49+
)
4850
```
4951

5052
Inside the `class extends Class` block, you can author custom element logic that you might want to make reusable across a multitude of elements. You can also adjust the input type to subclass `CustomElementClass`, which can be useful for setting up a contract between your Ability and the classes that rely on it:
@@ -58,10 +60,20 @@ interface Fooable {
5860
foo(): void // This interface has additional methods on top of `CustomElementClass`!
5961
}
6062

63+
interface FooableClass {
64+
new(...args: any[]): Fooable
65+
}
66+
6167
// Fooable: automatically calls `foo()` on `connectedCallback`
6268
export const fooable = createAbility(
63-
<T extends CustomElementClass & { new (): Fooable }>(Class: T): T =>
69+
// ↓ Notice the `& FooableClass`
70+
<T extends CustomElementClass & FooableClass>(Class: T): T =>
6471
class extends Class {
72+
// TypeScript will expect the constructor to be defined for a mixin
73+
constructor(...args: any[]) {
74+
super(...args)
75+
}
76+
6577
connectedCallback() {
6678
// Classes that apply this ability _must_ implement `foo()`.
6779
super.foo()

0 commit comments

Comments
 (0)