TypeScript: Interface vs Class vs Modules vs Program vs Function


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.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值