Emberjs and Typescript SetUp

It's hard for me to get typescript and ember to work together. I got all the definition files in definitely printed , and I went through ToDo, going through the Ember guide on the site. I am trying to convert js to typescript and see what works best for setting up a project, but I think I don't understand the definition of typescript very well.

If I do this:

/// <reference path="typings/ember.d.ts" /> var App = Ember.Application.create(); 

App is the type '{}', and I cannot access the Routers to do the next line of the manual.

 App.Router.map( ... ) 

The best I have found on the Internet is this , which does not work with the current typing.

I saw the typescript ember-app set, but it really doesn't help, since it barely includes any typescript, and their setup is almost not like ember guides. I just have to be directed in the right direction. Thank you, guys.

+7
typescript
source share
1 answer

I am not familiar with Ember, but by checking ember.d.ts, I see that create () is defined as a static universal function for an object:

 static create<T extends {}>(arguments?: {}): T; 

So you should be able to get the best type information by passing the actual type:

 var App = Ember.Application.create<Ember.Application>(); 

However, I also see that the ember typedef does not include the "Router" element in the application class, and even if it is, the Router class does not define map (). I was able to get it working by creating an extended type definition:

 // ./EmberExt.d.ts /// <reference path="typedef/ember/ember.d.ts" /> declare class RouterExt extends Ember.Router { map: ItemIndexEnumerableCallbackTarget; } declare class ApplicationExt extends Ember.Application { Router: RouterExt; } 

And then referring to this from my combined router / application file:

 /// <reference path="typedef/ember/ember.d.ts" /> /// <reference path="./EmberExt.d.ts" /> var App = Ember.Application.create<ApplicationExt>(); App.Router.map(function () { this.resource('todos', { path: '/' }); }); 

After that, it compiles and loads without errors, although it actually does nothing (which, in my opinion, is suitable for this phase of the walkthrough)

+5
source share

All Articles