错误 TS2339:类型 'Promise<void>' 上不存在属性'finally'



我正在使用一些JHipster生成的服务来增强我的登录组件,但我遇到了一个问题。

当我尝试提交登录表单时,我得到了error TS2339: Property 'finally' does not exist on type 'Promise<void>'.

以下是生成错误的代码:login.component.ts

login() {
if (this.validate(this.form)) {
this.loginService
.login({
username: this.model.username,
password: this.model.password,
})
.then(() => {
this.redirectUser();
})
.catch(() => {
this.authNoticeService.setNotice('The username or password is incorrect', 'error');
})
.finally(() => {
this.spinner.active = false;
this.actionChange.next( this.action );
});
}
}

登录.service.ts

login(credentials, callback?) {
const cb = callback || function() {};
return new Promise((resolve, reject) => {
this.authServerProvider.login(credentials).subscribe(
data => {
this.principal.identity(true).then(account => {
// After the login the language will be changed to
// the language selected by the user during his registration
if (account !== null) {
this.languageService.changeLanguage(account.langKey);
}
resolve(data);
});
return cb();
},
err => {
this.logout();
reject(err);
return cb(err);
}
);
});
}

我试着从login.service.ts添加返回类型的登录方法,比如:

login(credentials, callback?):Promise<any> {

但没有任何成功。

我做了一些研究,并据此:https://stackoverflow.com/a/52098456/9026582应使用CCD_ 3来解决该问题
我有typescript: 2.7.2版本,所以我想情况并非如此。

也许问题与login(credentials, callback?)方法中的回调有关?

EDIT:原始tsconfig.json配置

tsconfig.json

{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"typeRoots": [
"./node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}

tsconfig.app.json

{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}

finallyes2018规范的一部分。您需要将target设置为2018,或者需要包含es2018库。

"compilerOptions": {
"target": "es2018",
...
}

"compilerOptions": {
"lib": [
"es2018",
"dom",
"scripthost"
]
...
}

这两个选项之间的区别在于,编译器是否会使用target选项发出与es2018兼容的JavaScript代码(即不会向下编译语言功能(,或者编译器是否只是假设规范的运行时功能存在(由指定的库定义(,但仍会将编译语言功能向下编译到您指定的任何目标(如果使用lib(

相关内容

最新更新