Why does Typescript use the keyword “export” to make classes and interfaces public?
Question:
While dabbling with Typescript I realised my classes within modules (used as namespaces) were not available to other classes unless I wrote the export keyword before them, such as:
module some.namespace.here
{
export class SomeClass{..}
}
So now I can use the above code like this:
var someVar = new some.namespace.here.SomeClass();
However I was just wondering why this keyword is used opposed to just using the public keyword which is used at method level to signify that a method or property should be externally accessible. So why not just use this same mechanism to make classes and interfaces etc externally visible?
This would give resulting code like:
module some.namespace.here
{
public class SomeClass{..}
}
---------------------------------------------------------------------------------------------------------------
Answers:
The primary reason is that export matches the plans for ECMAScript. You could argue that "they should have used "export" instead of "public", but asides from "export/private/protected" being
a poorly matched set of access modifiers, I believe there is a subtle difference between the two that explains this.
In TypeScript, marking a class member as public or private has no effect on the generated JavaScript. It is simply a design / compile time tool that you can use to stop your TypeScript code accessing things it shouldn't.
With the export keyword, the JavaScript adds a line to add the exported item to the module. In your example: here.SomeClass = SomeClass;.
So conceptually, visibility as controlled by public and private is just for tooling, whereas the export keyword changes the output.
Why does Typescript use the keyword “export” to make classes and interfaces public?的更多相关文章
- Parsing error: The keyword 'export' is reserved && error Parsing error: Unexpected token <
如果你也在使用eslint,也报了如上错误,可以尝试: $ npm install babel-eslint --save-dev 然后,加上: rules: { "parser" ...
- 使用Hbuilder 报错The keyword 'export' is reserved
右击文件 > 验证本文档语法(V)后报错 解决: 项目右键->”属性”->”语法&框架”界面中配置项目的javaScript版本,将ECMAScript5.1 修改为6.
- [Typescript] What is a Function Type ? Function Types and Interfaces - Are They Related ?
Function Type: type messageFn = (name: string) => string; function sayHello(name: string): string ...
- ASP.NET MVC(三) TypeScript
TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScript与JavaScript相比 ...
- [转载]TypeScript 入门指南
之前有听过,但未使用过,而最近在用nodejs,angularjs做一些前端项目,想到了这个来,正是学习TypeScript的时候,看介绍貌似和coffeescript相似,也JavaScript的转 ...
- 译文:TypeScript新手指南
你是否听过 TypeScript? TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeS ...
- TypeScript 入门指南
你是否听过 TypeScript? TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeS ...
- Override is not allowed when implementing interface method Bytecode Version Overriding and Hiding Methods
java - @Override is not allowed when implementing interface method - Stack Overflow https://stackove ...
- 10 Essential TypeScript Tips And Tricks For Angular Devs
原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...
随机推荐
- git 免密码配置
1.cd ~/ 2.touch .git-credentials (注意文件名前面有个 ”点”) 3.打开刚刚创建的文件,写入 https://username:password@github. ...
- 使用AppCompat项目模版
使用AppCompat项目模版 从Android API 22开始,谷歌推荐使用AppCompatActivity来构建带标题栏的App,而不是原有的ActionBarActivity.如果用户想 ...
- Hibernate 多对一注解
在前面学习了基于配置文件的多对一关系,而在实际的开发过程中我们更多的是使用注解去开发.在这里来简单学习一下基于注解的多对一关系. 1. 创建所需要的实体 注:这里需要特别注意的是,如果使用的是mysq ...
- Python字典树实现
class Trie: # word_end = -1 def __init__(self): """ Initialize your data structure he ...
- [CSAcademy]A-Game
题目大意: 给你一个只含字符'A'和'B'的串,A和B两人轮流对其中的子串染色,要求被染色的子串中不包含已经被染色的子串. 最后,如果一方染的'A'少,那么这一方胜: 如果双方染的'A'和'B'一样多 ...
- Codeforces Beta Round #10 D. LCIS 动态规划
D. LCIS 题目连接: http://www.codeforces.com/contest/10/problem/D Description This problem differs from o ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) E. Bear and Contribution 单调队列
E. Bear and Contribution 题目连接: http://www.codeforces.com/contest/658/problem/E Description Codeforce ...
- iOS11 获取手机已安装应用列表
在iOS 11 以前我们可以使用LSApplicationWorkspace来获取手机上已安装的应用列表 iOS 11 上获取所有已安装应用接口被禁,但可以根据BundleId检查App是否存在 - ...
- 直接拿来用!最火的iOS开源项目(一~三)
结束了GitHub平台上“最受欢迎的Android开源项目”系列盘点之后,我们正式迎来了“GitHub上最受欢迎的iOS开源项目”系列盘点.今天,我们将介绍20个在GitHub上非常受开发者欢迎的iO ...
- 设计模式 - 观察者模式(Observer Pattern) Java内置 用法
观察者模式(Observer Pattern) Java内置 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26601659 ...