Angular + TypeScript + external modules. How to save file type information?

I am using Angular 1.4, TypeScript 1.6 and CommonJS plugins.

I have three files in this module:

paginator.module.ts
paginator.service.ts
paginator.controller.ts

paginator.module.ts as follows:

import PaginatorService = require('./paginator.service');
import PaginatorCtrl = require('./paginator.controller');

var Paginator = angular.module('paginator', [])
  .service('pageService', PaginatorService)
  .controller('pageCtrl', PaginatorCtrl);

Here paginator.service.ts:

class PaginatorService {
    currentPage: number;
    pageCount: number;
    pages: Array<Array<any>>;

    constructor() {
      this.currentPage = 0;
    }
  }

export = PaginatorService;

As paginator.controller.tsI put pageServicetogether with $scopeusing dependency injection, and I expect to see information about the type, when I give him the type PaginatorService.

class PaginatorController {
  scope: ng.IScope;
  pageService: PaginatorService;

  /**
   * @param  $scope
   * @param  pageService
   */
  constructor($scope: ng.IScope, pageService: PaginatorService) {
    this.scope = $scope;
    this.pageService = pageService;
  }
 export = PaginatorController;

However, I get the following error in atom-typescript:

TS Error: Cannot find name 'PaginatorService'.

What i tried

  • Adding another import statement to the file paginator.controller.jsfixes the problem (i.e. import PaginatorService = require('./paginator.service');). However, this seems redundant since I already require()d this service when setting up my Angular module.

  • tsc --declaration paginator.service.js , , .

Autogenerated d.ts file

declare class PaginatorService {
    currentPage: number;
    pageCount: number;
    pages: Array<Array<any>>;
    constructor();
}
export = PaginatorService;
  • ( ?), , , 100 + Angular , CommonJS, .

: CommonJS + Angular + TypeScript, , Angular , ?

!

+4
1

TS: "PaginatorService".

export class PaginatorService 

ES6

import {PaginatorService} from "./paginatorService"; 

import paginatorService = require("./paginatorService"); 
import PaginatorService = paginatorService.PaginatorService; 
+1

Source: https://habr.com/ru/post/1619429/


All Articles