Skip to content

Commit 36d7ff6

Browse files
authored
Merge pull request #2119 from elwayman02/patch-1
2 parents 35587a0 + a371d07 commit 36d7ff6

File tree

1 file changed

+3
-2
lines changed
  • guides/release/typescript/additional-resources

1 file changed

+3
-2
lines changed

guides/release/typescript/additional-resources/gotchas.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export default class MyGame extends Component {
8282
Let's imagine a component which just logs the names of its arguments when it is first constructed. First, we must define the [Signature][] and pass it into our component, then we can use the `Args` member in our Signature to set the type of `args` in the constructor:
8383

8484
```typescript {data-filename="app/components/args-display.ts"}
85+
import type Owner from '@ember/owner';
8586
import Component from '@glimmer/component';
8687

8788
const log = console.log.bind(console);
@@ -95,7 +96,7 @@ export interface ArgsDisplaySignature {
9596
}
9697

9798
export default class ArgsDisplay extends Component<ArgsDisplaySignature> {
98-
constructor(owner: unknown, args: ArgsDisplaySignature['Args']) {
99+
constructor(owner: Owner, args: ArgsDisplaySignature['Args']) {
99100
super(owner, args);
100101
Object.keys(args).forEach(log);
101102
}
@@ -104,7 +105,7 @@ export default class ArgsDisplay extends Component<ArgsDisplaySignature> {
104105

105106
Notice that we have to start by calling `super` with `owner` and `args`. This may be a bit different from what you're used to in Ember or other frameworks, but is normal for sub-classes in TypeScript today. If the compiler just accepted any `...arguments`, a lot of potentially _very_ unsafe invocations would go through. So, instead of using `...arguments`, we explicitly pass the _specific_ arguments and make sure their types match up with what the super-class expects.
106107

107-
The types for `owner` here and `args` line up with what the `constructor` for Glimmer components expects. The `owner` is specified as `unknown` because this is a detail we explicitly _don't_ need to know about. The `args` are the `Args` from the Signature we defined.
108+
The types for `owner` here and `args` line up with what the `constructor` for Glimmer components expects. The `owner` is specified as `Owner`, imported from the `@ember/owner` module. The `args` are the `Args` from the Signature we defined.
108109

109110
Additionally, the types of the arguments passed to subclassed methods will _not_ autocomplete as you may expect. This is because in JavaScript, a subclass may legally override a superclass method to accept different arguments. Ember's lifecycle hooks, however, are called by the framework itself, and thus the arguments and return type should always match the superclass. Unfortunately, TypeScript does not and _cannot_ know that, so we have to provide the types directly.
110111

0 commit comments

Comments
 (0)