简体   繁体   English

如何将 module.exports 对象结构从 javascript 重写为 typescript

[英]How to rewrite module.exports object structure from javascript to typescript

I am in a process of rewriting my node.js app in typescript, and I've stumbled across a bunch of index.js files, which have a following construct:我正在用 typescript 重写我的 node.js 应用程序,我偶然发现了一堆 index.js 文件,它们具有以下结构:

module.exports = {
  query: require('./query'),
  mutation: require('./mutation')
}

which is later imported like this: const { query } = require './module';后来像这样导入: const { query } = require './module';

I tried to rewrite it like this:我试着像这样重写它:

import {query} from './query'
import {mutation} from './mutation'

export default {
  query,
  mutation
}

but then I can't access the query object by import {query} from './query';但是我无法通过import {query} from './query';

How can I successfully convert the following structure to ts, so that I don't have to import the whole module?怎样才能成功将下面的结构转换成ts,这样就不用导入整个模块了? Thanks!谢谢!

you need to do export { query, mutation, }你需要做 export { query, mutation, }

default is for only one export general like object默认仅用于一个导出通用对象

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

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