Home > Net >  TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type arg
TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type arg

Time:12-21

I just upgraded my app from Angular 11 to 12. When I do an ng serve, I get the following error

Error: src/app/workers/cache.worker.ts:51:29 - error TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?

74                 resolve();
                   ~~~~~~~~~

  node_modules/typescript/lib/lib.es2015.promise.d.ts:33:34
    33     new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
                                        ~~~~~~~~~~~~~~~~~~~~~~~~~
    An argument for 'value' was not provided.

This is the code block for the error:

let result = new Promise(resolve => {
        localForage.getItem(id).then((storageData: StorageInterface) => {
            if (storageData && storageData.value) {
                try {
                    let response = pako.inflate(storageData.value, {level: 6});
                    let decodedString = JSON.parse(new TextDecoder('utf-8').decode(response));
                    storageData.value = decodedString;
                    resolve(storageData);
                } catch (e) {
                    console.log(e.message);
                    resolve();
                }
            }
            resolve();
        });
    });

To handle this, I tried adding a tag before the Promise declaration as

new Promise<void>(resolve => {
...

But now, I get the following error

Error: src/app/workers/cache.worker.ts:51:29 - error TS2345: Argument of type 'StorageInterface' is not assignable to parameter of type 'void | PromiseLike<void>'.
  Property 'then' is missing in type 'StorageInterface' but required in type 'PromiseLike<void>'.

51                     resolve(storageData);
                               ~~~~~~~~~~~

  node_modules/typescript/lib/lib.es5.d.ts:1489:5
    1489     then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    'then' is declared here.

How do I handle this case where I have to pass a parameter in the try block, but no parameter in the catch block?

CodePudding user response:

I do believe that working code will looks like this (pay attention to the new Promise<StorageInterface | undefined> :

type StorageInterface = { value: string };
let result = new Promise<StorageInterface | undefined>((resolve) => {
  localForage.getItem(id).then((storageData: StorageInterface) => {
    if (storageData && storageData.value) {
      try {
        let response = pako.inflate(storageData.value, { level: 6 });
        let decodedString = JSON.parse(
          new TextDecoder("utf-8").decode(response)
        );
        storageData.value = decodedString;
        resolve(storageData);
      } catch (e) {
        console.log(e.message);
        resolve(undefined);
      }
    } else {
      resolve(undefined);
    }
  });
});

BUT: I would suggest to use the reject to propagate errors and make possible to handle them in the proper way


type StorageInterface = { value: string };
let result = new Promise<StorageInterface>((resolve, reject) => {
  localForage.getItem(id).then((storageData: StorageInterface) => {
    if (storageData && storageData.value) {
      try {
        let response = pako.inflate(storageData.value, { level: 6 });
        let decodedString = JSON.parse(
          new TextDecoder("utf-8").decode(response)
        );
        storageData.value = decodedString;
        resolve(storageData);
      } catch (e) {
        console.log(e.message);
        reject(e.message);
      }
    } else {
      reject(e.message);
    }
  });
});

  • Related