4

Can someone please explain why this works in typescript while exporting an object:

export const config={
port:4000
};

This also works:

const config = { port:4000 };
export { config };

But this gives an error

const config={
  port:4000
};
export config;

Error : declaration or statement expected.

2
  • 2
    im just curious to understand how typescript 'export' differs from node.js 'exports' Commented Aug 31, 2017 at 15:12
  • typescript export is about the same as javascript export which is a newer, better language feature, designed to replace node exports but somewhat incompatible with it. Commented Aug 31, 2017 at 15:41

1 Answer 1

2

export expects a type object or curly braces. The second version is a syntax error.

If you want to export just a config objects then do

export const config = { port:4000 };

From the docs:

This can also be written as export {config};

Sign up to request clarification or add additional context in comments.

3 Comments

but isint config an object here? Im sorry im just new with this
config is an object but your export statement was syntactically incorrect. You need curly braces to export. Also you can export directly by putting export before declaration like mentioned above. @noob7
as a followup, export default const x = ()=>console.log("abs"); also doesnt work but, const x = ()=>console.log("abs"); export default x; works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.