【问题标题】:Typescript: Make class in function known to ts打字稿:使ts知道函数中的类
【发布时间】:2016-02-28 00:18:39
【问题描述】:

所以我在角度工厂函数中定义了我的类:

export default function ($resource, $http:angular.IHttpService) {
  return class Account{}
}

我还有一个 all.ts,其中包括了我的肯定类型。当我想在另一个类中使用 Account 时,我将其注入:

class AccountsController {
  public static $inject = ['Account'];
  constructor(public Account:Account){}
}

但是,Account 是未知的,即使我将工厂文件添加到 all.ts 引用,它也找不到它。

我怎样才能让打字稿知道 Account,理想情况下无需再次定义接口(因为它已经通过类定义了,不是吗?)。

【问题讨论】:

    标签: angularjs typescript


    【解决方案1】:

    我通常这样做:

    export function theService(...): theService {
      return {
        theServiceMethod1: function(id) {
      }
    }
    
    export declare interface theService {
      theServiceMethod1(id): void;
      ....
    }
    

    同样的方法也可以应用于一个类。类成员是已知的,但导出函数的返回类型不知道。

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 2021-10-25
      • 2012-11-06
      • 2022-01-01
      • 2022-11-28
      • 2021-06-30
      • 2021-03-17
      • 2020-05-02
      相关资源
      最近更新 更多