Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//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); 
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading