What is the difference between exporting and publishing in typescript?

I think this is another bias on this issue . And maybe the question is better worded when will you use the public, not export? From my reading, it seems that any C # / Java person thinks publicly that you really want to export.

When / where will you use public instead of export?

+6
source share
2 answers

public technically does nothing as a visibility modifier (all members of the class are public by default); it exists as an explicit analogue of private . This right is only within classes.

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

At the top level of the file, export means that the containing file is an external module (i.e., it will be loaded using the RequireJS command, Node require or some other CommonJS / AMD compatible bootloader) and that the character you put export on must be an exported member of this external module.

Inside the module export block means that the specified element is visible outside this module. By default, the objects in the module blocks are “privacy closure” - objects that do not have visibility are not visible outside the module. When the declaration inside the module has the export modifier, it instead becomes a property of the module object, which can be accessed from outside the module.

There is no place in the language where both public and export are legal, so the choice is relatively simple in this regard.

+12
source

export designed specifically for modules, for example:

 module foo{ export var bar; } 

public refers to classes / methods, for example:

 class Foo{ public bar = 123; } 

If you want to know more about the modules, I made a video about this: http://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

+1
source

Source: https://habr.com/ru/post/958349/


All Articles