Exporting a function on typescript "declaration or statement expected"

Typescript

Typescript Problem Overview


I realize this is really simple but typescript seems to have changed a lot in the last years and i just cant get this done with previous answers i found here on stack overflow.

let myfunction = something that returns a function

export myfunction;

I get an error "declaration or statement expected"

How can i export a function from a really simple ts file to be able to use the function in another ts file?

Typescript Solutions


Solution 1 - Typescript

It seems that

let myfunction = something that returns a function
export {myfunction};

will do the trick.

Solution 2 - Typescript

Use

export default myfunction

if you only have this function to export from this file. Otherwise use

export { myfunction, <other exports> }

to export myfunction along with other types to export

Solution 3 - Typescript

You can call a function or instantiate a class from another file using modular top-level import and export declarations.

file1.ts

// This file is an external module because it contains a top-level 'export'
export function foo() {
    console.log('hello');
}
export class bar { }

file2.ts

// This file is also an external module because it has an 'import' declaration
import f1 = module('file1');
f1.foo();
var b = new f1.bar();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoaquin BrandanView Question on Stackoverflow
Solution 1 - TypescriptJoaquin BrandanView Answer on Stackoverflow
Solution 2 - TypescriptPhilip BijkerView Answer on Stackoverflow
Solution 3 - TypescriptjrbedardView Answer on Stackoverflow