I'm currently trying to clear some code to program against interfaces, not implementations, but I can't figure out how to do this.
To be more specific, I use: * TypeScript 1.5.0 beta -> passed to ES5 / commonjs * SystemJS to load modules
I am currently trying to use external modules as follows:
posts.service.ts file:
///<reference path="../../../typings/tsd.d.ts" /> ///<reference path="../../../typings/typescriptApp.d.ts" /> ... export interface PostsService{ // 1 fetchPosts(): Rx.Observable<any>; } export var PostsService:PostsServiceImpl; // 2 ... export class PostsServiceImpl implements PostsService { // 3 ... constructor(){ console.log('Loading the Posts service'); } ... fetchPosts(): Rx.Observable<any>{ ... }
and this module is imported into posts.ts:
///<reference path="../../../typings/tsd.d.ts" /> ///<reference path="../../../typings/typescriptApp.d.ts" /> import {PostsService, PostsServiceImpl} from 'components/posts/posts.service'; @Component({ selector: 'posts', viewInjector: [ //PostsServiceImpl //PostsService bind(PostsService).toClass(PostsServiceImpl) ] }) ... export class Posts { private postsServices: PostsService; constructor(postsService: PostsService) { console.log('Loading the Posts component'); this.postsServices = postsService; ... } ... }
The above code compiles correctly, but basically my problem is that Angular will not insert an instance of PostsServiceImpl into the Posts component.
Of course, this is simply because I did not find the right way to declare / export / import everything
Without the export interface PostsService ... , I get TS compilation errors because I use it in a message controller.
Without export var PostsService:PostsServiceImpl; I get TS compilation errors in @View decorInjector constructor view
In the generated js code, I find only exports.PostsService; , which is added due to var export ... I know that the interfaces disappear at runtime.
So I'm a little lost in all of this. Any practical ideas on how I can use w / TypeScript and Angular 2 based programming?
angular typescript systemjs
dSebastien
source share