我有这个代码,结果和错误都有错误:
src / app / login / phone / phone.component.ts(48,75)中的错误:错误TS7006: 参数'结果'隐含地有一个“任何”的类型。 src / app / login / phone / phone.component.ts(53,14):错误TS7006:参数 '错误'隐含地有一个“任何”的类型。
verifyLoginCode() {
this.windowRef.confirmationResult.confirm(this.verificationCode).then(result => {
this.user = result.user;
})
.catch(error => console.log(error, "Incorrect code entered?"));
}
如何解决?
我正在使用angularfire2,angular5。
答案 0 :(得分:2)
此错误的原因是因为Angular的tsconfig.json
默认情况下将 noImplicitAny 标记设置为 true - "noImplicitAny": true,
。有了这个就生成了Js代码,但是你也得到了错误,因为编译器无法推断出类型。
最简单的修复方法是then((result: any)
现在,在你的评论中,你提到你试过then((result: String)
。我打赌你真正的意思是then((result: string)
string
和String
不一样。 string
是一个Javascript原语,使用文字创建 - ''
或""
,而String
是一个Javascript 对象及其原型链。
为了将来参考,您只需通过console.log' inig就可以轻松检查类型(如果您不能通过其他方式了解):
.then(result => {
console.log(typeof result)
})
P.S。并且因为this.user = result.user;
显然result
不是字符串,而是Object
的某些字符。
答案 1 :(得分:-1)
您的打字稿设置不允许您指定变量而不声明其类型。在此示例中,then方法接受参数“result”,但没有说出TYPE“result”是什么。
将功能更新为
.then((result: ResultType) => {
其中ResultType
是任何结果的正确类型。
此外,在catch
回调中,error
未获得类型。您还需要将(error =>
更改为((error: SomeType) =>
。
实际上您可能想要禁用noImplicitAny
tsconfig
中的compilerOptions
选项。你可能不想这样做有很多原因(这会迫使你对你的类型更加明确,从长远来看这可能是好事),但如果你只是在试验或试图学习,那么你可能不会想要选项集。
如果这个项目是为了工作,或者是由其他人管理(即你只是项目中众多开发人员之一),那么你可能不想禁用它,因为他们可能想要noImplicitAny
因为他们自己的原因(代码质量)。