首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误TS2339:属性'Promise<void>‘在’Promise<void>‘类型上不存在

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

Stack Overflow用户
提问于 2018-11-08 08:36:11
回答 1查看 9K关注 0票数 3

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

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

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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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添加返回类型的登录方法,如下所示:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
{
  "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

代码语言:javascript
运行
复制
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-08 08:40:48

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

代码语言:javascript
运行
复制
"compilerOptions": {
    "target": "es2018",
     ...
}

代码语言:javascript
运行
复制
"compilerOptions": {
    "lib": [
        "es2018",
        "dom",
        "scripthost"
    ]
   ...
}

这两个选项的不同之处在于编译器是否会发出与es2018兼容的target代码(即它不会向下编译语言特性)与target选项兼容,或者编译器是否只是假设规范的运行时特性存在(由指定的lib定义),但它仍然会将编译语言特性降到您指定的任何目标(如果使用lib)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53204047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档