I'm trying to create a .d.ts file for a js package so I can have typescript import functions from that package. The package is using CommonJs style to define its exported functions. (Not an es6 file.)
The package's main property from its package.json
is directed to file: index.js
. The index.js
file is displayed as:
'use strict'
const Plugins = require('./Plugins')
module.exports = {
plugins: Plugins
}
From the Plugis.js file:
module.exports.function1 = function1() {// function1 content};
module.exports.function2 = function2() {// function2 content};
module.exports.function3 = function3() {// function3 content};
In pure JavaScript, this works, and I can use any of const Plugins
exported functions.
I'm trying to create a typescript version index.d.ts with a similar make up:
'use strict'
import Plugins from './Plugins'
module.exports = {
plugins: Plugins
}
However, I get errors when I import the js package and attempt to use the exported functions. For example, can use plugins.function1()
. I've been researching this topic and followed several examples, but I keep getting errors, such as if I declare the function I want to use, and examples use imports from es6 style files. what is correct approach to import all file's functions?
question from:
https://stackoverflow.com/questions/65641018/typescript-how-to-import-all-export-functions-from-a-js-file-and-then-declare-a 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…