AngularJS类型脚本服务错误
我在尝试向模块添加服务时遇到此错误。有人能帮我指出哪里出了问题吗 Angular 1.5.11和TypeScript 2.2.2AngularJS类型脚本服务错误,angularjs,typescript,angular-services,Angularjs,Typescript,Angular Services,我在尝试向模块添加服务时遇到此错误。有人能帮我指出哪里出了问题吗 Angular 1.5.11和TypeScript 2.2.2 ERROR in ./source/mainModule.ts (185,33): error TS2345: Argument of type 'typeof AwesomeService ' is not assignable to parameter of type 'Injectable<Function>'. Type 'typeof Awes
ERROR in ./source/mainModule.ts
(185,33): error TS2345: Argument of type 'typeof AwesomeService ' is not
assignable to parameter of type 'Injectable<Function>'.
Type 'typeof AwesomeService ' is not assignable to type '(string | Function)[]'.
Property 'push' is missing in type 'typeof AwesomeService '.
在另一个文件中,下面是我创建服务的方式
export default angular.module('iris.service', [])
/* This line throws the error --> */.service('awesomeService', AwesomeService);
export class AwesomeService extends OtherClass {
private static $inject = ['configService'];
constructor() {
super();
}
}
更新:
我发现,如果我将AwesomeService更改为一个函数并导出它,它就可以正常工作。有什么方法可以使用类进行服务吗?看起来@types/angular指定angular.module.service的第二个参数应该是一个字符串或一个函数。是的,您可以完全按照自己的意愿执行,只需编写更少的代码即可
@types/angular
中的类型声明包括以下内容
declare global {
interface Function {
$inject?: ReadonlyArray<string>;
}
}
更详细的答案是,如果一个声明了一个私有成员(要做到这一点,它需要是一个类),而另一个声明了一个公共成员(所有接口成员都是公共的),那么这两种类型在结构上是不兼容的
不幸的是,这个错误有点神秘。
private$inject
声明导致类型检查器在调用服务
时立即删除函数
目标类型。然后,它尝试匹配数组,但失败。如果像这样大写“AwesomeService”,该怎么办:导出默认角度.module('iris.service',[]).service('AwesomeService',AwesomeService)代码>这不会改变任何事情。这可以是任意的名字。:)好吧,我想一想later@Spencer是的,对注册名称和注册值使用相同大小写的唯一好处是,您可以编写.module('iris.service',[]).service({AwesomeService})
这是一种方便的速记方式,特别是当您使用某些注册模式时。太棒了。我想我想到的是传输JavaScript,其中公共/私有都变成了相同的awesome服务。$inject
。谢谢你帮我记住打字脚本更聪明!很乐意帮忙:)
export class AwesomeService extends OtherClass {
static $inject = ['configService']; // no private here.
constructor() {
super();
}
}