How to export a function type?

Viewed 12875

This is how I export and import typescript interface for objects. Everything works just fine. Giving this as an example of what I'm trying to achieve, but with functions.

Module 1

export interface MyInterface {
  property: string;
}

Module 2

import {MyInterface} from './module1';

const object: MyInterface = {
    property: 'some value'
};

The code below gives me an error "TS2304: Cannot find name 'myFunction'". How do I export and import a function type?

Module 1

export let myFunction: (argument: string) => void;

Module 2

import {myFunction} from './module1';

let someFunction: myFunction; 
2 Answers

In the case you need a return type of the function type as well, you should create it in the following manner (as explained in the TypeScript handbook):

Module 1

export interface myFunction {
    (argument: string): boolean;
}

Module 2

import {myFunction} from './module1';

let someFunction: myFunction; 
let result = someFunction("hello");
Related