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
For example, if you use an external script that defines
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
varcreates a new variable.
declareis 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 externalModuleto hint to the TypeScript compiler that
externalModulehas been correctly set up.
相关文章推荐
- interface vs abstract class
- interface vs abstract class
- program interface module class 区别
- Java abstract class vs interface
- Abstract class VS Interface
- Abstract class VS Interface
- Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class
- 模式乱写之2 : 接口VS抽象类 Interface VS Abstract Class
- Programming in Java: Multi-threads and interface vs abstract class
- Programming in Java: Multi-threads and interface vs abstract class
- java interface vs abstract class
- Some function to handle class and its interface info
- A rule of thumb about Interface vs Abstract Class
- Programming in Java: Multi-threads and interface vs abstract class
- Django CBV(Class-Base View) vs FBV(Function-Base View)
- [TypeScript] Interface and Class
- abstract class VS interface
- Programming in Java: Multi-threads and interface vs abstract class
- Abstract class VS Interface 转载
- Interface VS Abstract Class