manidos
manidos

Reputation: 3464

How to export a function type?

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; 

Upvotes: 12

Views: 19583

Answers (2)

mPrinC
mPrinC

Reputation: 9401

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");

Upvotes: 0

manidos
manidos

Reputation: 3464

This is how it's done:

Module 1

export type myFunction = (arg: string) => void

Module 2

import {myFunction} from './module1';

let someFunction: myFunction; 

Upvotes: 28

Related Questions