Can typescript export a function?
Asked Answered
E

5

74

Is it possible to export a simple function from a typescript module?

This isn't compiling for me.

module SayHi {
    export function() {
    console.log("Hi");
  }
}
new SayHi();

This workitem seems to imply that you cannot but doesn't flat out say it. Is it not possible?

Equiprobable answered 27/3, 2013 at 3:39 Comment(0)
O
101

It's hard to tell what you're going for in that example. exports = is about exporting from external modules, but the code sample you linked is an internal module.

Rule of thumb: If you write module foo { ... }, you're writing an internal module; if you write export something something at top-level in a file, you're writing an external module. It's somewhat rare that you'd actually write export module foo at top-level (since then you'd be double-nesting the name), and it's even rarer that you'd write module foo in a file that had a top-level export (since foo would not be externally visible).

The following things make sense (each scenario delineated by a horizontal rule):


// An internal module named SayHi with an exported function 'foo'
module SayHi {
    export function foo() {
       console.log("Hi");
    }

    export class bar { }
}

// N.B. this line could be in another file that has a
// <reference> tag to the file that has 'module SayHi' in it
SayHi.foo();
var b = new SayHi.bar();

file1.ts

// This *file* is an external module because it has a top-level 'export'
export function foo() {
    console.log('hi');
}

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

file1.ts

// This will only work in 0.9.0+. This file is an external
// module because it has a top-level 'export'
function f() { }
function g() { }
export = { alpha: f, beta: g };

file2.ts

// This file is also an external module because it has an 'import' declaration
import f1 = require('file1');
f1.alpha(); // invokes f
f1.beta(); // invokes g
Overburdensome answered 27/3, 2013 at 4:57 Comment(2)
Thanks, the exports function blah() syntax is what I was looking forEquiprobable
Four years later, I believe it would make much more sense if "export" was called "public" instead...Maharani
J
16

To answer the title of your question directly because this comes up in Google first:

YES, TypeScript can export a function!

Here is a direct quote from the TS Documentation:

"Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword."

Reference Link

Jakejakes answered 9/12, 2018 at 10:30 Comment(0)
A
13

If you are using this for Angular, then export a function via a named export. Such as:

function someFunc(){}

export { someFunc as someFuncName }

otherwise, Angular will complain that object is not a function.

Edit: I'm using angular 11 now and this isn't needed anymore. So it's enough to export function someFunc(){ ...}

Arleta answered 1/9, 2019 at 9:35 Comment(0)
N
2

In my case I'm doing it like this:

 module SayHi {
    export default () => { console.log("Hi"); }
 }
 new SayHi();
Noeminoesis answered 9/12, 2018 at 10:37 Comment(0)
L
0

You can also use the from keyword with import and destruct the exported object directly.

file1.ts

export const CARS_QUERY = `
    {
      getAllCars {
        model,
        make,
        picture
      }
    }
    `;

file2.ts

import { CARS_QUERY } from "file1.ts";

Lacerta answered 25/2, 2022 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.