Export vs. Default Exports TypeScript

TypeScript - Export vs. Default Exports

What is export in TypeScript? Why using export in TypeScript?

Export - Exporting a declaration
Any variable, function, class or interface can be exported by using the export keyword. After using export keyword, you can access your variable, function, class or interface to outside the module.

Example –
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 becaues using export.
    export class ExportedClass {
        public subNumbers(a: number, b: number): number {
            return a - b;
        }
    }
}

//AND 
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;
        }
    }
}


What is default export in TypeScript? Why using default export in TypeScript?

Default exports – 
Each module can optionally export a default export and the default exports work with the keyword default and we can use only one default export per module.

Example -

export class User {
    //Todo your logic here..
}

And then -
import { User } from "./User";

//OR

//BaseUrl.ts
export default "http://localhost:57431/Account/Login";

//Login.ts
import BaseUrl from "../BaseUrl";
console.log(BaseUrl); //"http://localhost:57431/Account/Login"

Stayed Informed – Learn Angular 2 with TypeScript

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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.