您的位置:首页 > 其它

TypeScript: Interface vs Class vs Modules vs Program vs Function

2014-03-19 01:16 369 查看
I would like to explain the Interface,Class,Module, Program and Fucntion in TypeScript per comparing C# and hopefully the answers are useful to people coming to TypeScript from similar languages too.

Interface

An interface in TypeScript is similar to those you have come across in C#. It is a contract - if one of your classes implements an interface, it promises to have certain properties or methods that the interface documents.

In TypeScript an interface can inherit from another interface in order to extend it and from a class to capture its implementation.

Whenever something seems impossible in TypeScript, you can usually solve it with an interface!

Class

This is very similar to the concept of a class in C#. You can inherit from other classes to extend or specialise the behaviour.

Module

Modules are analogous to C# namespaces. They allow you to group a number of classes together into a logical group. A module can also have functions and variables alongside classes.

Program

A program is a collection of modules, classes. This is essentially the thing you have written using TypeScript.

Function

A TypeScript function is just like a C# method.

Declare vs. var

var
creates a new variable.
declare
is used to tell TypeScript that the variable has been created elsewhere. If you use
declare
, nothing is added to the JavaScript that is generated - it is simply a hint to the compiler.

For example, if you use an external script that defines
var externalModule
, you would use
declare externalModule
to hint to the TypeScript compiler that
externalModule
has been correctly set up.


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: