简体   繁体   English

将 nodejs 样式的导出转换为 typescript 样式的导出

[英]Convert nodejs-style export to typescript-style export

So, in Node.js to export primary and "secondary" functions I would do:因此,在 Node.js 中导出主要和“次要”功能我会做:

 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.但现在我需要将此模块从 Node.js 转换为 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.但是当我调用“主要”function 时,它给了我错误。

How do I do it the right way?我该如何以正确的方式做到这一点?

import * as amountix from './amountix';

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

Or或者

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

amountix(20);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM