Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript : Type 'Promise<{}>' is not assignable to type 'Promise<void>'

I have the following function :

let templateLoader = (onDidFinishLoad : Function, onDidFailLoad : Function) =>
    (url : string) : Promise<void> => 
        new Promise(    
            (resolve,reject) => {
                mainWindow.loadURL(url);
                mainWindow.webContents.once(
                    'did-finish-load',
                    () => { 
                        onDidFinishLoad(resolve);
                    }  
                );  
                mainWindow.webContents.once(
                    'did-fail-load', 
                    (event,errorCode,errorDescription) => {  
                        onDidFailLoad(reject,errorDescription); 
                    }  
                );   
            } 
        ); 

I've got the following compilation error:

ERROR in [at-loader] ./app/loaders.ts:9:9 TS2322: Type 'Promise {}' is not assignable to type Promise void. Type '{}' is not assignable to type 'void'.

like image 761
Anatoly Strashkevich Avatar asked Jul 28 '17 10:07

Anatoly Strashkevich


People also ask

Is not assignable to type '() => void?

The "Type 'void' is not assignable to type" TypeScript error occurs when we forget to return a value from a function, so the function gets an implicit return type of void . To solve the error, make sure you return a value of the correct type from your functions before the assignment.

How do you handle a promise void?

When a function definition has a void return type, it means the function must not return anything. If you need to resolve the Promise with a value of another type, pass the specific type to the generic. Copied! The Promise above can only be resolved with a number.

What is promise void?

promise<void> may be used to create future<void> objects which have no value. In addition, a promise<void> can be constructed from a promise<T> where T is any type. This allows a promise<void> to be used by code which only needs to be able to renege on a promise and not fulfill it.


1 Answers

it works by modifying : Promise<void> into : Promise<any>,

or to cast new Promise into new Promise<void>.

like image 94
Val Avatar answered Oct 18 '22 04:10

Val