我尝试将现有的角度应用程序迁移到typescript(版本1.5.3):
这是代码:
'use strict'; angular.module('x') .directive('tabsPane',TabsPane) function TabsPane(itemTabs) { return { restrict: 'E',compile: compileTabs }; function compileTabs(tElement) { var template = createTabsTemplate(); //function which i don't include here for brevity tElement.append(template); }}
当我编译javascript我得到:
error TS2345: Argument of type ‘(itemTabs: any) => { restrict: string; compile: (tElement: any) => void; }’ is not assignable to parameter of type ‘any[]’.
Property ‘push’ is missing in type ‘(itemTabs: any) => { restrict: string; compile: (tElement: any) => void; }’.
我试着理解为什么它在这里抱怨我去了angular的typescript定义:
不知何故,打字稿暗示了这个定义
directive(name: string,inlineAnnotatedFunction: any[]): IModule;
以下定义更合适:
directive(name: string,directiveFactory: IDirectiveFactory): IModule;
我是打字稿的新手,所以我认为我做错了,但我找不到任何与谷歌相关的东西.
我从旧的javascript angularJs 1.4.9代码迁移到typescript 2.6代码时遇到了同样的问题.我正在使用@ types / angular ver 1.6.39.我添加了’any’作为参数函数的返回类型,如:
原文链接:https://www.f2er.com/angularjs/141805.htmlangular.module("app") .directive("tabsPane",function TabsPane(itemTabs): any { return { restrict: 'E',compile: compileTabs }; function compileTabs(tElement) { var template = createTabsTemplate(); tElement.append(template); } });
错误消失了:)