Ember-cli-typescript: Casting Ember.inject.service() resolutions

Created on 3 Oct 2017  Â·  3Comments  Â·  Source: typed-ember/ember-cli-typescript

Hi,

Thanks for an excellent addon - eagerly awaiting the next steps of development here.

I have a usage question: how can I formulate some code like this and get it to properly understand that this.store is indeed of the DS.Store type?

class WorkspaceService extends Ember.Object {
    session = Ember.inject.service();
    store = Ember.inject.service();

    someMethod() {
        // The line below will generate a compilation error
        this.store.findRecord('workspace', 1);
    }

I _can_ do like this, which gets incredibly ugly and type-unsafe. But what better way could there be? (unless creating this object outside of Ember and passing in the store instance as a constructor parameter or similar...)

    someMethod() {
        let store = <DS.Store><any>this.store;
        this.store.findRecord('workspace', 1);
    }

Most helpful comment

Hi @perlun – using the new typings we've been working on over in typed-ember/ember-typings (which is where most of our efforts have been concentrated for the last six weeks or so), you can do what you want:

import EmberObject from '@ember/object';
import { inject as service } from '@ember/service';
import CP from '@ember/object/computed';
import DS from 'ember-data';
import Session from 'your-app/services/session';

class WorkspaceService extends EmberObject {
    session: CP<Session> = service();
    store: CP<DS.Store> = service();

    someMethod() {
        // should work, but you *might* need to do `this.get('store')` instead.
        this.store.findRecord('workspace', 1);
    }

    someOtherMethod {
        const someProp = this.get('session').get('someSessionProperty');
    }
}

Can you try that out with the new typings over there and let us know if that does indeed resolve this? If not, we have more work to do over there.

All 3 comments

Hi @perlun – using the new typings we've been working on over in typed-ember/ember-typings (which is where most of our efforts have been concentrated for the last six weeks or so), you can do what you want:

import EmberObject from '@ember/object';
import { inject as service } from '@ember/service';
import CP from '@ember/object/computed';
import DS from 'ember-data';
import Session from 'your-app/services/session';

class WorkspaceService extends EmberObject {
    session: CP<Session> = service();
    store: CP<DS.Store> = service();

    someMethod() {
        // should work, but you *might* need to do `this.get('store')` instead.
        this.store.findRecord('workspace', 1);
    }

    someOtherMethod {
        const someProp = this.get('session').get('someSessionProperty');
    }
}

Can you try that out with the new typings over there and let us know if that does indeed resolve this? If not, we have more work to do over there.

Hi @perlun – using the new typings we've been working on over in typed-ember/ember-typings (which is where most of our efforts have been concentrated for the last six weeks or so), you can do what you want:

Sounds great! I'm actually already using the new typings, since they were more up-to-date with the Ember classes. (Ember.computed.union was missing in the DefinitelyTyped repo)

Can you try that out with the new typings over there and let us know if that does indeed resolve this?

Yep, will do that during the day and keep you posted.

I'm closing this as I've confirmed it works locally. We do need to finish getting the ember-data typings solidly ready to use, though, so that e.g. DS.Store methods will type-check properly with Ember Data Models (they're… pretty weird, today).

Was this page helpful?
0 / 5 - 0 ratings