TypeScript: Interface vs Class vs Modules vs Program vs Function

Typescript

Typescript Problem Overview


I read the TypeScript specification located at: http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf

However it got me confused with following:

  1. Interface
  2. Class
  3. Modules
  4. Programs
  5. Functions.
  6. Declare vs. var

Could someone briefly help to understand which one of above should be used when? Is Interface and Class same as C# version?

Typescript Solutions


Solution 1 - Typescript

I have made these answers match C# as you've mentioned that in your question, but 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!

In TypeScript, interfaces have a broad range of uses. They describe a structure, so can be used anywhere you use a type (i.e. not just to implement them in a class, you can use them to type variables, parameters, return values and so on).

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.

Namespace

The newer namespace keyword is used to place a group of code within a limited scope. This is similar to C# namespaces.

Module

Modules are better than namespaces when it comes to TypeScript. A module (previously known as an external module) is a file that is self contained and adds nothing to your global scope. You can load modules into local variables as you need them. Modules provide a good way to organise your code and load parts on demand. When using modules, it is best to avoid using namespaces. Modules are better than namespaces.

Program

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

Function / Method

Classes contain methods, and you can also write stand-alone functions that do not belong to a class.

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 var externalModule to hint to the TypeScript compiler that externalModule has already been set up.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNil PunView Question on Stackoverflow
Solution 1 - TypescriptFentonView Answer on Stackoverflow