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/ ------------------------- ...
随机推荐
- Python导入模块-Import
#1语法importimport module1,module2,module3,module4 #2from xx import xx 语句from module import name1,name ...
- Django+Nginx+uwsgi搭建自己的博客(四)
由于在上篇博文中仍然介绍了相当多的后端部分,导致原定于上篇介绍的前端部分“跳票”到了这篇.在此篇博文中,我将会介绍Users App和主页的前端部分,从而形成我们博客的一个雏形. 在前端部分,我们主要 ...
- zookpeer应用和zkclient实践
分布式 zkclient 排它锁 在需要获取排它锁时,通过调用create()接口,创建临时子节点.zk会保证在所有客户端中,只有一个会创建成功,从而获取锁. 其他客户端注册该节点的变更watch监听 ...
- 阿里云提示Discuz memcache+ssrf GETSHELL漏洞如何解决
一般这个漏洞都是下面文件,source/function/function_core.php 搜索下面代码: $content = preg_replace($_G['setting']['outpu ...
- NOIP2017 D1T3逛公园
DP+最短路 两遍最短路判零环 DP转移f[i][j] 到点i的距离比最短路多j时的方案数 #include<bits/stdc++.h> using namespace std; ; s ...
- EMGU 2.9.X在VS2010下调试真正靠谱的配置
emgu有多强大或者干什么的网上找资料吧.这里就说说我在2010下的配置的经历. 在网上找了很多资料,有细节到一步一步的,但我跟着弄还是没有成功.比如修改cpu,复制emgu的bin目录下某些所需文件 ...
- [原]Redis使用场景及使用经验
Redis is an open source (BSD licensed), in-memory data structure store! 欢迎转载,转载请注明出处 刚刚结束一个游戏类的活动项目, ...
- [转]web服务器apache架构与原理 &apache 监控
web服务器 在开始了解Apache前,我 ...
- 【转】Internet连接正常但是没有网络,禁用以太网以后再重新启动就可以使用了,原因是什么?
只是粘贴别人的答案,觉得有理,就放在博客里方便以后再学习~ 这个和网络中hdcp服务有关,网卡要在网路中通讯就必须要网络设备一般是路由器或者交换机分配地址,只有给了你电脑门牌号,信件投递能准确无误.你 ...
- Object-C—集合
Obejct-C中包含了三种集合,分别是:数组.字典和集(set). 数组和C语言中的数组相似,但是OC中的数组只能存储对象,不能存储基本数据类型,如int.float.enum.stru ...