importing and calling a function in Typsescript which returns nothing

Hi I have a function in typescript which returns nothing. When I try to import and call this function in another part of my app, I am getting errors. I am fairly new to typescript and am struggling with how to fix this issue.

Here’s my code. I am trying to set up a few scripts to do some simple tests (would prefer not to use any testing frameworks).

My helper functions for testing are here

//File: helper.ts
type validator = () => void;
export const it = (desc: string, fn: validator) => {
    try {
        let res = fn();
        console.log("\x1b[32m%s\x1b[0m", `\u2714 ${desc}`);
    } catch (error) {
        console.log("\n");
        console.log("\x1b[31m%s\x1b[0m", `\u2718 ${desc}`);
        console.error(error);
    }
};

My tests use the helpers and are defined like this;

// File: dummytests.ts
import { strict as assert } from 'node:assert';
import { it } from "src/spec/helper";
export const check_if15_eqls_15 = it("shoulld check if something is true", () => { 
    assert.strictEqual(15, 15); 
});

and i finally am running the tests as such;

// File: testRUnner.ts
import {check_if15_eqls_15} from 'src/spec/frontend/dummyTests';

console.log ("This test harness is for the frontend tests.\n");
check_if15_eqls_15();

this throws the error;

 error TS2349: This expression is not callable.
  Type 'void' has no call signatures.

5 check_if15_eqls_15();
  ~~~~~~~~~~~~~~~~~~

>Solution :

The line

export const check_if15_eqls_15 = it("shoulld check if something is true", () => { 
    assert.strictEqual(15, 15); 
});

already calls the method.

This means check_if15_eqls_15 is already the return value of the method and unless the return value is not another function (which it isn’t in this case), you can’t call it again.

The same thing would happen in pure JS, since TypeScript does not change how the code is run

Something that might work for your example would be this:

export const check_if15_eqls_15 = () => it("shoulld check if something is true", () => { 
    assert.strictEqual(15, 15); 
});

Leave a Reply