TS 2345 error "Type 'void' is not assignable to type '{}'"



This site utilizes Google Analytics, Google AdSense, as well as participates in affiliate partnerships with various companies including Amazon. Please view the privacy policy for more details.

So earlier today I had defined a little validation function type in TypeScript:

type validated =  (valid: boolean) => {};

In my mind, the above type describes a function that takes a boolean and returns nothing. After all, it looks similar to the following JavaScript function:

valid => {};

But when I tried to use it, I got a TS 2345 error: Type 'void' is not assignable to type '{}'.

Um, ok, aren’t I returning nothing… As in, void?

Well, no, those double-curly brackets were more like an empty Object and less the brackets around an empty function body.

So I was saying I was essentially returning something like the following:

let object = {};

The solution? change () => {} to () => void:

type validated =  (valid: boolean) => void;

Leave a Reply

Note that comments won't appear until approved.