Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between export and public in typescript?

I think this is a different slant on this question. And maybe the question is better phrased, when would you use public, as opposed to export? From my reading it seems like anywhere a C#/Java person thinks public, what you actually want is export.

When/where would you use public instead of export?

like image 976
David Thielen Avatar asked Nov 19 '13 00:11

David Thielen


People also ask

What does export mean in TypeScript?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

What is the difference between export and exports?

If you categories exports as 'export of domestic goods' and export of 'foreign goods', the difference between export and re export makes easier to follow. In simple terms, exports mean export of domestic goods moved out to a foreign country.

How do I export a TypeScript file?

Use named exports to export a function in TypeScript, e.g. export function sum() {} . The exported function can be imported by using a named import as import {sum} from './another-file' . You can use as many named exports as necessary in a single file.

Can I export type in TypeScript?

Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

What's the difference between module exports and exports?

When we want to export a single class/variable/function from one module to another module, we use the module. exports way. When we want to export multiple variables/functions from one module to another, we use exports way. 2.


1 Answers

public as a visibility modifier technically does nothing (all class members are public by default); it exists as an explicit counterpart to private. It's legal only inside classes.

export does two different things depending on its context (on a top-level member in a file or in a module block).

At the top level of a file, export means that the containing file is an external module (i.e. it will be loaded using RequireJS, Node's require command, or some other CommonJS/AMD-compliant loader) and that the symbol you put export on should be an exported member of that external module.

Inside a module block, export means that the specified member is visible outside that module block. The default for things in module blocks is "closure privacy" -- unexported objects are not visible outside the module. When a declaration inside a module has the export modifier, it instead becomes a property of the module object that can be accessed from outside the module.

There is no place in the language where both public and export are legal, so choosing is relatively easy in that regard.

like image 54
Ryan Cavanaugh Avatar answered Sep 29 '22 08:09

Ryan Cavanaugh