【问题标题】:error TS2339: Property 'finally' does not exist on type 'Promise<void>'错误 TS2339:“Promise<void>”类型上不存在属性“finally”
【发布时间】:2019-04-11 17:50:48
【问题描述】:

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

当我尝试提交登录表单时收到error TS2339: Property 'finally' does not exist on type 'Promise&lt;void&gt;'.

这是产生错误的代码: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 );
            });
    }
}

login.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 问题应该通过typescript: 2.7.0 解决。
我有typescript: 2.7.2版本,所以我猜不是这样。

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

编辑:原始 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"
  ]
}

【问题讨论】:

标签: angular typescript promise jhipster


【解决方案1】:

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

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

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

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

【讨论】:

  • tsconfig.json中的"lib": [ "es2017", "dom" ]替换为"lib": [ "es2018", "dom" ],重启了服务器,但没有成功:/
  • @rupertoss 这应该是真正的问题.. 你能发布你当前的 tsconfig 吗?你用的是什么版本的ts?
  • 编辑问题并添加原始 tsconfig.json 文件
  • @rupertoss 与您的 tsconfig(使用 2017)我得到了错误。当我将其更改为 2018 时,vs 代码中的错误会立即消失。也许您没有编辑正确的 tsconfig ?
  • 我不知道为什么,但这对我不起作用。这只是 finally 块中的代码行,所以我在 then 和 catch 中都添加了它们。我知道这不是一个好的解决方案,但它确实有效。您的答案似乎显然是正确的,尤其是当您说您已经检查过的时候。它只是在我的机器上不起作用。
猜你喜欢
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 2019-01-21
  • 2016-08-13
  • 1970-01-01
  • 2016-12-31
相关资源
最近更新 更多