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
Looking at that example above, Typescript knows what types `this.args` has, but how? In the `constructor` version, we explicitly _named_ the type of the `args` argument. Here, it seems to just work automatically. This works because the type definition for a Glimmer component looks roughly like this:
132
+
Now, looking at that bit of code, you might be wondering how it knows what the type of `this.args` is. In the `constructor` version, we explicitly _named_ the type of the `args` argument. Here, it seems to just work automatically. This works because the type definition for a Glimmer component looks roughly like this:
Not sure what’s up with `<Args>`or `<S>`_at all_? We highly recommend the [TypeScript Deep Dive](https://basarat.gitbooks.io/typescript/) book’s [chapter on generics ](https://basarat.gitbooks.io/typescript/docs/types/generics.html) to be quite helpful in understanding this part.
143
+
Not sure what’s up with `<Args>` _at all_? We highly recommend the [TypeScript Deep Dive](https://basarat.gitbooks.io/typescript/) book’s [chapter on generics ](https://basarat.gitbooks.io/typescript/docs/types/generics.html) to be quite helpful in understanding this part.
142
144
{% endhint %}
143
145
144
146
The type signature for Component, with `Argsextends {} = {}`, means that the component _always_ has a property named `args` —
@@ -156,6 +158,44 @@ let b = ["hello", "goodbye"]; // Array<string>
156
158
157
159
In the case of the Component, we have the types the way we do so that you can’t accidentally define `args` as a string, or `undefined` , or whatever: it _has_ to be an object. Thus, `Component<Argsextends {}>` . But we also want to make it so that you can just write `extendsComponent` , so that needs to have a default value. Thus, `Component<Argsextends {} = {}>`.
158
160
161
+
### Giving `args` a type
162
+
163
+
Now let’s put this to use. Imagine we’re constructing a user profile component which displays the user’s name and optionally an avatar and bio. The template might look something like this:
Assuming the default `tsconfig.json` settings \(with `strictNullChecks:true`\), this wouldn't type-check if we didn't _check_ whether the `bio` argument were set.
198
+
159
199
## Generic subclasses
160
200
161
201
If you'd like to make your _own_ component subclass-able, you need to make it generic as well.
@@ -177,4 +217,3 @@ export default class FancyInput<Args extends FancyInputArgs = FancyInputArgs> ex
177
217
```
178
218
179
219
Requiring that `ArgsextendsFancyInputArgs` means that subclasses can have _more_ than these args, but not _fewer_. Specifying that the `Args = FancyInputArgs` means that they _default_ to just being `FancyInputArgs`, so users don't need to supply an explicit generic type parameter here unless they're adding more arguments to the class.
0 commit comments