Max Mikhalchuk
Max Mikhalchuk

Reputation: 105

Convert nodejs-style export to typescript-style export

So, in Node.js to export primary and "secondary" functions I would do:

module.exports = (amount) => {
  return amount
}

module.exports.calc = (amount) => {
  return amount * amount
}

module.exports.stringify = (amount) => {
  return String(amount)
}

And then call it from other files like so:

const amountix = require('./amountix')

amountix(20) // 20
amountix.calc(20) // 400
amountix.stringify(20) // "20"

But now I need to convert the this module from Node.js to TypeScript. I tried the following:

export default (amount: number): number => {
  return amount;
}

export function calc(amount: number): number {
  return amount * amount;
}

export function stringify(amount: number): string {
  return String(amount);
}

import * as amountix from "./amountix.ts"

amountix(20) // ERROR: This expression is not callable.
amountix.calc(20) // 400
amountix.stringify(20) // "20"

But it gives me error when I call the "primary" function.

How do I do it the right way?

Upvotes: 0

Views: 178

Answers (1)

IXKfIq5mrL2B
IXKfIq5mrL2B

Reputation: 71

import * as amountix from './amountix';

// call the default method
amountix.default(20);

Or

import amountix, { calc, stringify } from './amountix';

amountix(20);

Upvotes: 1

Related Questions