Skip to content

Commit 7555df2

Browse files
authored
Merge branch 'master' into update-npmignore
2 parents fb65897 + e30f6f5 commit 7555df2

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

docs/SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* [Decorators](ts/decorators.md)
99
* [Current limitations](ts/current-limitations.md)
1010
* [Building Addons in TypeScript](ts/with-addons.md)
11-
* [Understanding the `@types` Package Names](ts/package-names.md)
11+
* [Understanding the @types Package Names](ts/package-names.md)
1212
* [Working With Ember](ember/README.md)
1313
* [Components](ember/components.md)
1414
* [Services](ember/services.md)
@@ -28,4 +28,3 @@
2828
* [Upgrading from 1.x](upgrade-notes.md)
2929
* [Troubleshooting](troubleshooting/README.md)
3030
* [Conflicting Type Dependencies](troubleshooting/conflicting-types.md)
31-

docs/ember/routes.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,19 @@ export default class MyRoute extends Route {
3434
}
3535
```
3636

37-
The `Resolved<T>` utility type takes in any type, and if the type is a `Promise` it transforms the type into whatever the `Promise` resolves to; otherwise it just returns the same type. As we saw above, `ReturnType` gets us the return type of the function. So our final `MyRouteModel` type takes the return type from our `model` hook, and uses the `Resolved` type to get the type the promise will resolve to—that is, exactly the type we will have available as `@model` in the template and as `this.model` on a controller. This in turn allows us to use
37+
The `Resolved<T>` utility type takes in any type, and if the type is a `Promise` it transforms the type into whatever the `Promise` resolves to; otherwise it just returns the same type. (If you’re using TypeScript 4.5 or later, you can use the built-in `Awaited<T>` type, which does the same thing but more robustly: it also handles nested promises.) As we saw above, `ReturnType` gets us the return type of the function. So our final `MyRouteModel` type takes the return type from our `model` hook, and uses the `Resolved` type to get the type the promise will resolve to—that is, exactly the type we will have available as `@model` in the template and as `this.model` on a controller.&#x20;
3838

39+
This in turn allows us to use the route class to define the type of the model on an associated controller.
40+
41+
```typescript
42+
import Controller from '@ember/controller';
43+
import type { MyRouteModel } from '../routes/my-route';
44+
45+
export default class MyController extends Controller {
46+
declare model?: MyRouteModel;
47+
48+
// ...
49+
}
50+
```
51+
52+
Notice here that the `model` is declared as optional. That’s intentional: the `model` for a given controller is _not_ set when the controller is constructed (that actually happens _either_ when the page corresponding to the controller is created _or_ the first time a `<LinkTo>` which links to that page is rendered). Instead, the `model` is set on the controller when the corresponding route is successfully entered, via its `setupController` hook.

0 commit comments

Comments
 (0)