原文: https://stackoverflow.com/questions/15760462/why-does-typescript-use-the-keyword-export-to-make-classes-and-interfaces-publ

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?的更多相关文章

  1. Parsing error: The keyword 'export' is reserved && error Parsing error: Unexpected token <

    如果你也在使用eslint,也报了如上错误,可以尝试: $ npm install babel-eslint --save-dev 然后,加上: rules: { "parser" ...

  2. 使用Hbuilder 报错The keyword 'export' is reserved

    右击文件 > 验证本文档语法(V)后报错 解决: 项目右键->”属性”->”语法&框架”界面中配置项目的javaScript版本,将ECMAScript5.1 修改为6.

  3. [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 ...

  4. ASP.NET MVC(三) TypeScript

    TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScript与JavaScript相比 ...

  5. [转载]TypeScript 入门指南

    之前有听过,但未使用过,而最近在用nodejs,angularjs做一些前端项目,想到了这个来,正是学习TypeScript的时候,看介绍貌似和coffeescript相似,也JavaScript的转 ...

  6. 译文:TypeScript新手指南

    你是否听过 TypeScript? TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeS ...

  7. TypeScript 入门指南

    你是否听过 TypeScript? TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeS ...

  8. 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 ...

  9. 10 Essential TypeScript Tips And Tricks For Angular Devs

    原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...

随机推荐

  1. thinkphp5.0配置作用域

    作用域 配置参数支持作用域的概念,默认情况下,所有参数都在同一个系统默认作用域下面.如果你的配置参数需要用于不同的项目或者相互隔离,那么就可以使用作用域功能,作用域的作用好比是配置参数的命名空间一样. ...

  2. elementUI 学习入门之 radio 单选框

    Radio 单选框 基础用法 选项默认可见,选项不宜过多,选项过多建议使用 select 选择器 使用 Radio 组件,需要设置 v-model 绑定变量,选中意味着变量的值为相应 Radio  l ...

  3. 转:Exploiting Electron RCE in Exodus wallet

    转:https://hackernoon.com/exploiting-electron-rce-in-exodus-wallet-d9e6db13c374 Exploiting Electron R ...

  4. 转:XSS知识大总结

    转:https://www.jianshu.com/p/75a3d9332b8c XSS知识大总结 2016.10.28 21:05* 字数 1332 阅读 961评论 2喜欢 13 XSS-即Cro ...

  5. Python并发编程-队列

    队列 IPC = Inter-Process Communication 队列 先进先出 队列的几种方法 #put() #full() #get() #empty() #get-nowait() fr ...

  6. 二分+并查集【bzoj3007】[SDOI2012]拯救小云公主

    Description 英雄又即将踏上拯救公主的道路-- 这次的拯救目标是--爱和正义的小云公主. 英雄来到boss的洞穴门口,他一下子就懵了,因为面前不只是一只boss,而是上千只boss.当英雄意 ...

  7. Bzoj5188/洛谷P4185 [Usaco2018 Jan]MooTube(并查集)

    题面 Bzoj 洛谷 题解 最暴力的方法是直接判两个点之间的路径最小值是否\(\geq k\),用\(Dijkstra\)可以做到该算法最快效率,但是空间复杂度始终是\(O(n^2)\)的,会\(ML ...

  8. 工厂bean和bean工厂

    FactoryBean(工厂bean):是bean的加工工厂,是对已知Bean的加工,是一个接口,要实现三个方法: ① Object getObject()可以对bean进行加工添加功能. ② Cla ...

  9. 【BZOJ 4305】 4305: 数列的GCD (数论)

    4305: 数列的GCD Description 给出一个长度为N的数列{a[n]},1<=a[i]<=M(1<=i<=N).  现在问题是,对于1到M的每个整数d,有多少个不 ...

  10. uoj22 【UR #1】外星人

    link 题意: 给一个长为n的序列a[],现在有一个初始值m,问一个1~n的排列p[],满足将m对a[p[i]]顺次取模后得到的值最大,输出最大值和方案数. $n,m\leq 5\times 10^ ...