File tree Expand file tree Collapse file tree 4 files changed +56
-0
lines changed
packages/@ember/controller
type-tests/@ember/controller-test Expand file tree Collapse file tree 4 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -366,3 +366,29 @@ export function inject(
366
366
}
367
367
368
368
export { Controller as default , ControllerMixin } ;
369
+
370
+ /**
371
+ A type registry for Ember `Controller`s. Meant to be declaration-merged so string
372
+ lookups resolve to the correct type.
373
+
374
+ Blueprints should include such a declaration merge for TypeScript:
375
+
376
+ ```ts
377
+ import Controller from '@ember/controller';
378
+
379
+ export default class ExampleController extends Controller {
380
+ // ...
381
+ }
382
+
383
+ declare module '@ember/controller' {
384
+ export interface Registry {
385
+ example: ExampleController;
386
+ }
387
+ }
388
+ ```
389
+
390
+ Then `@inject` can check that the service is registered correctly, and APIs
391
+ like `owner.lookup('controller:example')` can return `ExampleController`.
392
+ */
393
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
394
+ export interface Registry extends Record < string , Controller | undefined > { }
Original file line number Diff line number Diff line change
1
+ // This module provides an 'extension' to the `@ember/owner` module from the
2
+ // `@ember/controller` module. Our type publishing infrastructure will pass it
3
+ // through unchanged, so end users will get this extension.
4
+
5
+ import type { Registry } from '@ember/controller' ;
6
+
7
+ declare module '@ember/owner' {
8
+ export interface DIRegistry {
9
+ controller : Registry ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change @@ -72,3 +72,14 @@ expectTypeOf(inject()).toMatchTypeOf<PropertyDecorator>();
72
72
73
73
// @ts -expect-error Doesn't allow invalid types
74
74
inject ( 1 ) ;
75
+
76
+ class ExampleController extends Controller { }
77
+
78
+ declare module '@ember/controller' {
79
+ export interface Registry {
80
+ example : ExampleController ;
81
+ }
82
+ }
83
+
84
+ expectTypeOf ( owner . lookup ( 'controller:example' ) ) . toEqualTypeOf < ExampleController > ( ) ;
85
+ expectTypeOf ( owner . lookup ( 'controller:non-registered' ) ) . toEqualTypeOf < Controller | undefined > ( ) ;
Original file line number Diff line number Diff line change 1
1
import Controller , { ControllerQueryParam , inject } from '@ember/controller' ;
2
+ import { expectTypeOf } from 'expect-type' ;
3
+ import type Owner from '@ember/owner' ;
2
4
3
5
class FirstController extends Controller {
4
6
foo = 'bar' ;
@@ -36,3 +38,9 @@ declare module '@ember/controller' {
36
38
second : InstanceType < typeof SecondController > ;
37
39
}
38
40
}
41
+
42
+ const owner = { } as Owner ;
43
+
44
+ expectTypeOf ( owner . lookup ( 'controller:first' ) ) . toEqualTypeOf < FirstController > ( ) ;
45
+ expectTypeOf ( owner . lookup ( 'controller:second' ) ) . toEqualTypeOf < InstanceType < typeof SecondController > > ( ) ;
46
+ expectTypeOf ( owner . lookup ( 'controller:non-registered' ) ) . toEqualTypeOf < Controller | undefined > ( ) ;
You can’t perform that action at this time.
0 commit comments