Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a function type?

Tags:

typescript

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; 
like image 542
manidos Avatar asked May 12 '17 07:05

manidos


People also ask

Can you export a function?

You can export as many functions as needed as long as you remember that there can be only one default export. The default export in JavaScript is used to export a single/fallback value from a module. With a default export, you do not need to specify a name for the exported function. The filename is used by default.

Can you export function of class?

By the use of an export keyword, we can export class, function, file, interface, type, etc. in TypeScript. By default it comes up with all the classes or interfaces we create in TypeScript, after this we can easily import these files using the 'import' keyword.

How do I export a function in React?

Use named exports to export multiple functions in React, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

What is export declare function?

The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the import declaration or dynamic import.


1 Answers

This is how it's done:

Module 1

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

Module 2

import {myFunction} from './module1';

let someFunction: myFunction; 
like image 148
manidos Avatar answered Oct 16 '22 18:10

manidos