error TS2339: Property 'finally' does not exist on type 'Promise<void>'

Viewed 8874

I am working on enhancing my Login component by using some JHipster generated services, but I've encountered a problem.

I am getting error TS2339: Property 'finally' does not exist on type 'Promise<void>'. when I try to submit the login form.

Here is the code which generates error: 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);
            }
        );
    });
}

I've tried to add returning type of login method from login.service.ts like that:

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

But without any success.

I've done some research and according to this: https://stackoverflow.com/a/52098456/9026582 the problem should be resolved with typescript: 2.7.0.
I have typescript: 2.7.2 version, so I guess that it is not the case.

Maybe the problem is connected to callbacks in login(credentials, callback?) method?

EDIT: original tsconfig.json configuration

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"
  ]
}
1 Answers

finally is part of the es2018 spec. You need to either set your target to 2018 or you need to include the es2018 lib.

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

or

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

The difference between the two option is whether the compiler will emit JavaScript code compatible with es2018 (ie it will not down-compile language features) with the target option or whether the compiler just assumes the runtime features of the spec exist (as defined by the specified libs) but it will still down-compile language features to whatever target you specify (if you use lib)

Related