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
Copy file name to clipboardExpand all lines: README.md
+75-23Lines changed: 75 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,7 @@
2
2
3
3
Use TypeScript in your Ember 2.x and 3.x apps!
4
4
5
-
[](https://travis-ci.org/typed-ember/ember-cli-typescript)
*["TypeScript is complaining about multiple copies of the same types"](#typescript-is-complaining-about-multiple-copies-of-the-same-types)
@@ -83,9 +84,7 @@ However, there are a few things worth noting if you're already familiar with Typ
83
84
84
85
## Using TypeScript with Ember effectively
85
86
86
-
In addition to the points made below, you may find the "Update" sequence in the [Typing Your Ember][typing-your-ember] blog series particularly helpful in knowing how to do specific things. In particular, [Update, Part 4][pt4] is a really important guide to making the service and controller injections and Ember Data lookups behave as described below.
In addition to the points made below, you may find the [Typing Your Ember][typing-your-ember] blog series (especially the "Update" sequence) particularly helpful in knowing how to do specific things.
89
88
90
89
### Incremental adoption
91
90
@@ -134,7 +133,66 @@ Along with the @types/ files mentioned above, ember-cli-typescript adds a starte
134
133
135
134
We install this file because the actual `config/environment.js` is (a) not actually identical with the types as you inherit them in the content of an application, but rather a superset of what an application has access to, and (b) not in a the same location as the path at which you look it up. We map it to the lookup path within your `types` directory, and TypeScript resolves it correctly.
136
135
137
-
### Service and controller injections
136
+
### String-keyed lookups
137
+
138
+
Ember makes heavy use of string-based APIs to allow for a high degree of dynamicism. With some limitations, you can nonetheless use TypeScript very effectively to get auto-complete/IntelliSense as well as to accurately type-check your applications.
139
+
140
+
The "Update" sequence in the Typing Your Ember has detailed explanations and guides for getting good type-safety for Ember's string-based APIs, e.g. the use of `get` and `set`, service and controller injection, Ember Data models and lookups
141
+
142
+
*[Part 1][pt1]: A look at normal Ember objects, "arguments" to components (and controllers), and service (or controller) injections.
143
+
*[Part 2][pt2]: Class properties — some notes on how things differ from the `Ember.Object` world.
144
+
*[Part 3][pt3]: Computed properties, actions, mixins, and class methods.
145
+
*[Part 4][pt4]: Using Ember Data, and service and controller injections improvements. (This includes a detailed guide to updating making the service and controller injections and Ember Data lookups behave as described below.)
A few of the most common speed-bumps are listed here to help make this easier:
153
+
154
+
#### `this` type workaround
155
+
156
+
One important note for using `class` types effectively with today's Ember typings: you will (at least for now) need to explicitly write out a `this` type for methods, computed property callbacks, and actions if you are going to use `get` or `set`
// `this` tells TS to use `UserProfile` for `get` and `set` lookups
165
+
}
166
+
}
167
+
```
168
+
169
+
This is a workaround for how incredibly dynamic `Ember.Object` instances are, and is only necessary but i; again, see [the relevant blog post for details][pt2].
170
+
171
+
#### Nested keys in `get` or `set`
172
+
173
+
In general, `this.get` and `this.set` will work as you'd expect _if_ you're doing lookups only a single layer deep. Things like `this.get('a.b.c')` don't (and can't ever!) type-check; see the blog posts for a more detailed discussion of why.
174
+
175
+
The workaround is simply to do one of two things:
176
+
177
+
1.**The type-safe approach.** This _will_ typecheck, but is both ugly and only works \*if there are no `null`s or `undefined`s along the way. If `nested` is `null` at runtime, this will crash!
2.**Using `// @ts-ignore`.** This will _not do any type-checking_, but is useful for the cases where you are intentionally checking a path which may be `null` or `undefined` anywhere long it.
187
+
188
+
```ts
189
+
// @ts-ignore
190
+
get(someObject, 'deeply.nested.key');
191
+
```
192
+
193
+
It's usually best to include an explanation of _why_ you're ignoring a lookup!
194
+
195
+
#### Service and controller injections
138
196
139
197
Ember does service and controller lookups with the `inject` helpers at runtime, using the name of the service or controller being injected up as the default value—a clever bit of metaprogramming that makes for a nice developer experience. TypeScript cannot do this, because the name of the service or controller to inject isn't available at compile time in the same way. This means that if you do things the normal Ember way, you will have to specify the type of your service or controller explicitly everywhere you use it.
140
198
@@ -221,7 +279,7 @@ You'll need to add that module and interface declaration to all your existing se
221
279
222
280
If you have a reason to fall back to just getting the `Service` or `Controller` types, you can always do so by just using the string-less variant: `service('session')` will check that the string is a valid name of a service; `session()` will not.
223
281
224
-
### Ember Data lookups
282
+
####Ember Data lookups
225
283
226
284
The same basic approach is in play for Ember Data lookups. As a result, once you add the module and interface definitions for each model, serializer, and adapter in your app, you will automatically get type-checking and autocompletion and the correct return types for functions like `findRecord`, `queryRecord`, `adapterFor`, `serializerFor`, etc. No need to try to write out those (admittedly kind of hairy!) types; just write your Ember Data calls like normal and everything _should_ just work.
227
285
@@ -279,7 +337,7 @@ In addition to the registry, note the oddly defined class for `DS.Model`s. This
Also notice that unlike with service and controller injections, there is no unsafe fallback method by default, because there isn't an argument-less variant of the functions to use as there is for `Service` and `Controller` injection. If for some reason you want to opt _out_ of the full type-safe lookup for the strings you pass into methods like `findRecord`, `adapterFor`, and `serializerFor`, you can add these declarations somewhere in your project:
285
343
@@ -303,7 +361,7 @@ declare module 'ember-data' {
303
361
304
362
However, we **_strongly_** recommend that you simply take the time to add the few lines of declarations to each of your `DS.Model`, `DS.Adapter`, and `DS.Serializer` instances instead. It will save you time in even the short run!
305
363
306
-
#### Fixing the Ember Data `error TS2344` problem
364
+
#####Fixing the Ember Data `error TS2344` problem
307
365
308
366
If you're developing an Ember app or addon and _not_ using Ember Data (and accordingly not even have the Ember Data types installed), you may see an error like this and be confused:
309
367
@@ -450,25 +508,19 @@ Here is the short list of things which do _not_ work yet in the version of the t
450
508
451
509
### Some `import`s don't resolve
452
510
453
-
You'll frequently see errors for imports which TypeScript doesn't know how to
454
-
resolve. For example, if you use Ember Concurrency today and try to import its
455
-
`task` helper:
511
+
You'll frequently see errors for imports which TypeScript doesn't know how to resolve. For example, if you use Ember Concurrency today and try to import its `task` helper:
456
512
457
513
```typescript
458
514
import { task } from'ember-concurrency';
459
515
```
460
516
461
-
You'll see an error, because there aren't yet type definitions for it. You may
462
-
see the same with some addons as well. **These won't stop the build from
463
-
working;** they just mean TypeScript doesn't know where to find those.
517
+
You'll see an error, because there aren't yet type definitions for it. You may see the same with some addons as well. **These won't stop the build from working;** they just mean TypeScript doesn't know where to find those.
464
518
465
-
Writing these missing type definitions is a great way to pitch in! Jump in
466
-
\#topic-typescript on the Ember Slack and we'll be happy to help you.
519
+
Writing these missing type definitions is a great way to pitch in! Jump in \#topic-typescript on the Ember Slack and we'll be happy to help you.
467
520
468
521
### Type safety when invoking actions
469
522
470
-
TypeScript won't detect a mismatch between this action and the corresponding
471
-
call in the template:
523
+
TypeScript won't detect a mismatch between this action and the corresponding call in the template:
0 commit comments