Ember: use type registry from service, controller lookups.

This commit is contained in:
Chris Krycho
2018-02-04 20:37:13 -07:00
parent ff4c5d83b6
commit 031e2ad65c
2 changed files with 30 additions and 4 deletions

View File

@@ -18,6 +18,18 @@ declare module 'ember' {
import Rsvp from 'rsvp';
import { TemplateFactory } from 'htmlbars-inline-precompile';
// A type registry for Ember `Service`s. Meant to be declaration-merged so
// string lookups resolve to the correct type.
export interface ServiceRegistry {
[serviceName: string]: Ember.Service;
}
// A type registry for Ember `Controller`s. Meant to be declaration-merged
// so string lookups resolve to the correct type.
export interface ControllerRegistry {
[controllerName: string]: Ember.Controller;
}
// Get an alias to the global Array type to use in inner scope below.
type GlobalArray<T> = T[];
@@ -2340,12 +2352,16 @@ declare module 'ember' {
* Creates a property that lazily looks up another controller in the container.
* Can only be used when defining another controller.
*/
function controller(name?: string): ComputedProperty<Controller>;
function controller<K extends keyof ControllerRegistry>(
name?: K
): ComputedProperty<ControllerRegistry[K]>;
/**
* Creates a property that lazily looks up a service in the container. There
* are no restrictions as to what objects a service can be injected into.
*/
function service(name?: string): ComputedProperty<Service>;
function service<K extends keyof ServiceRegistry>(
name?: K
): ComputedProperty<ServiceRegistry[K]>;
}
namespace ENV {
const EXTEND_PROTOTYPES: typeof Ember.EXTEND_PROTOTYPES;

View File

@@ -8,9 +8,19 @@ class ApplicationController extends Ember.Controller {
transitionToLogin() {}
}
declare module 'ember' {
interface ServiceRegistry {
auth: AuthService;
}
interface ControllerRegistry {
application: ApplicationController;
}
}
class LoginRoute extends Ember.Route {
auth = Ember.inject.service('authentication') as Ember.ComputedProperty<AuthService>;
application = Ember.inject.controller() as Ember.ComputedProperty<ApplicationController>;
auth = Ember.inject.service('auth');
application = Ember.inject.controller('application');
didTransition() {
if (!this.get('auth').get('isAuthenticated')) {