Skip to content

Commit 21f677e

Browse files
authored
Merge pull request #238 from buschtoens/patch-1
docs(README/service-injections): add ! operator
2 parents 5ae2ab0 + ed3626a commit 21f677e

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ import { service } from '@ember-decorators/service';
341341
import MySession from 'my-app/services/my-session';
342342

343343
export default class UserProfile extends Component {
344-
@service mySession: MySession;
344+
@service mySession!: MySession;
345345

346346
login(this: UserProfile, email: string, password: string) {
347347
this.session.login(email, password);
@@ -351,6 +351,8 @@ export default class UserProfile extends Component {
351351

352352
Note that we need the `MySession` type annotation this way, but we *don't* need the string lookup (unless we're giving the service a different name than the usual on the class, as in Ember injections in general). Without the type annotation, the type of `session` would just be `any`. This is because decorators (as of TS 2.8 – 3.0) are not allowed to modify the types of whatever they decorate. As a result, we wouldn't get any type-checking on that `session.login` call, and we wouldn't get any auto-completion either. Which would be really sad and take away a lot of the reason we're using TypeScript in the first place!
353353

354+
Also use the [`!` non-null assertion operator](https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#non-null-assertion-operator) to prevent [`TS2564`](https://github.com/kaorun343/vue-property-decorator/issues/81), that is caused by enabling `strictPropertyInitialization` in `tsconfig.json`.
355+
354356
You'll need to add that module and interface declaration to all your existing service and controller declarations for this to work (again, see the [blog post][pt4] for further details), but once you do that, you'll have this much nicer experience throughout! It's not quite vanilla Ember.js, but it's close—and this way, you still get all those type-checking and auto-completion benefits, but with a lot less noise! Moreover, you actually get a significant benefit over "vanilla" Ember: we type-check that you typed the key correctly in the `service` invocation.
355357

356358
#### Ember Data lookups

0 commit comments

Comments
 (0)