Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Interface vs Class vs Modules vs Program vs Function

Tags:

typescript

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?

like image 316
Nil Pun Avatar asked Oct 06 '12 21:10

Nil Pun


People also ask

What is the difference between an interface and a class in TypeScript?

TypeScript class vs. Classes are the fundamental entities used to create reusable components. It is a group of objects which have common properties. It can contain properties like fields, methods, constructors, etc. An Interface defines a structure which acts as a contract in our application.

What is the difference between class and interface in JS?

A class is a blueprint from which we can create objects that share the same configuration - properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.

What is the difference between interface and class?

A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. A class may contain abstract methods, concrete methods. An interface contains only abstract methods.

Should I use interface or type TypeScript?

Interfaces are most recommended for defining new objects or methods or properties of an object where it will receive a specific component. Hence interface works better when using objects and method objects. Therefore it is our choice to choose between types or interface according to the program needs.


1 Answers

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.

like image 172
Fenton Avatar answered Oct 04 '22 12:10

Fenton