Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Module vs. Namespace - TypeScript

Module vs. Namespace-
Module is for external packages and the namespace is for internal packages. Actually, the module keyword has been replaced with the namespace keyword.

Namespaces are simply named JavaScript objects in the global namespace.
Modules can contain both code and declarations. The main difference is that modules declare their dependencies.

Stayed Informed - Angular 2 Modules vs. JavaScript Modules 

The named modules called “namespace” in latest version of TypeScript. So we can use namespace instead of internal modules in the TypeScript.

As per me, this one is the best coding practice but don’t mind the internal modules are also supporting, if you want can use it.


///Example using module -
module System.modules {

//this function can be accessed from outside the module because using export.
export function addNumbers(a: number, b: number): number {
return a + b;
}

// this class can be accessed from outside the module because using export.
export class ExportedClass {
public subNumbers(a: number, b: number): number {
return a - b;
}
}

//this class can only be accessed from inside the module because not using export.
class NotExportedClass {
mulNumbers(a: number, b: number): number {
return a * b;
}

divNumbers(a: number, b: number): number {
return a > 0 ? a / b : 0;
}
}
}

///Example using namespace –
namespace System.namespaces {

//this function can be accessed from outside the module because using export.
export function addNumbers(a: number, b: number): number {
return a + b;
}

// this class can be accessed from outside the module because using export.
export class ExportedClass {
public subNumbers(a: number, b: number): number {
return a - b;
}
}

//this class can only be accessed from inside the module because not using export.
class NotExportedClass {
mulNumbers(a: number, b: number): number {
return a * b;
}

divNumbers(a: number, b: number): number {
return a > 0 ? a / b : 0;
}
}
}

I hope you are enjoying with this post! Please share with you friends. Thank you!!

Share the post

Module vs. Namespace - TypeScript

×

Subscribe to Angular 2 Javascript Asp.net C# Sql Server Knockoutjs Angularjs Kendo Ui Html 5 Web Api Wpf Wcf Etc.

Get updates delivered right to your inbox!

Thank you for your subscription

×