#export typescript
14 messages · Page 1 of 1 (latest)
can you elaborate on what exactly you're trying to achieve?
i have file folder/src/index.ts which contains export { demo, test } from '@/doing/stuff' can i import folder/src/index in other file in order to use functions demo and test
yes, that's what export is for
that's like, the single thing that statement allows you to do
alright
This helped me better understand how exports work:
(If I understand it correctly)
Every export is basically a default export
e.g.
exporter.ts
export default { foo: 'bar' }
importer.ts
import GiveAnyName from 'exporter' // default
// { foo: 'bar' }
import { foo } from 'exporter' // destructed
// foo = 'bar'
Hint. In JavaScript there is only 'default export':
module.exports = { foo: 'bar' }
i might be confused about what you're trying to get across, but if i have a file named ./exporter.js with this contents:
export default { foo: 'bar' }
and i try to import that via:
import { foo } from './exporter.js'
it doesn't work (at least with standardized ECMAScript modules, not sure about other module systems)
default exports are their own separate thing, and the import { … } from … syntax is not really the same as "destructuring" (though i see the analogy)
I'm sure it works on CommonJS, but let me check if it works iwth ES too.
// exporter
module.exports = { foo: 'bar' }
// importer
const { foo } = require('./exporter.js')
console.log(foo)
You are right
I had no idea
CJS is fundamentally different from ESM. i'm not sure any of this is relevant to just_follow_white_rabbit's original question though (which AFAICT was just confirming that export can be used to re-export stuff?)
in cjs, there's only a single export. esm, which ts module syntax is based on, has both default and named exports. esm does exist at runtime.