EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript declare module
 

TypeScript declare module

Updated April 6, 2023

TypeScript declare module

 

 

Introduction to TypeScript declare module

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains the data type and access modifiers like public, private, protected when we use both declare and module in the TypeScript language we should be initialised on the beginning line of the script also make it confirm the declare and module are accessible by the global if the access modifier value is in public.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

We know that TypeScript has default keywords, variables, and functions to implement the application with a more sophisticated nature. In that declare module has defined and executed with their own scopes and not across the global scope because it can be used with the inner side and not across the outside of the module. So the Access modifiers take the main role for these keywords.

declare module "module name" {
class or interface name {
--some typescript codes—
}
---some other default typescript codes depends upon the project requirement---

The above code we used module and declare in the TypeScript application. It may vary upon the user requirement and depend on the project; some classes, interface, and methods are imported using the “import” keyword and declared using the “declare” keyword. The return type will be returned on the function declaration. Finally, we can use it to declare the module as the specific module on the TypeScript.

Working of declare module in TypeScript

  • Basically, declare module is one of the modules, and it is a block for declaring the ambient set of modules and is also being called and used it for both compile and runtime. Mainly this module will always exist at the runtime mode, and also the module and export values are similar to the namespace declarations in the script codes; each and every declaration have a separate name and that it will use some parent and child classes along with the same module. Like that, we use some import and export modules that are additionally used in the declare module. If we use the import keyword in TypeScript, the specific packages will be imported on the applications.
  • Mainly the imported packages are classes and interfaces for the specific task in the application; both import and export statements are called and declared the files through global type; the script programs are also included in the compilation but without calling to explicitly so that the export keyword is used for to export the interfaces and classes also it exports the functions or methods which is based on the specific classes. Additionally, the functions or methods are also called and exported through the outer end of the script.

Examples of TypeScript declare module.

Given below are the examples of the TypeScript declare module:

Example #1

Test.d.ts

declare module 'first' {
type exam =  {
demo: string;
demo1: string;
demo2: string;
demo3: string;
demo4: string;
demo5: string;
demo6: string;
demo7: string;
demo8: string;
demo9: string;
demo10: string;
};
export class second{
constructor()
denos5():string;
}
let Exams:exam;
}

First.ts

import { Exams } from 'first';
console.log("welcome");

Output:

TypeScript declare module 1

The above example is the basic example for initialising the declare module in the test.d.ts file. Whenever we want to declare the module, we will save the code with the .d.ts extension. So that it will the TypeScript compiler knows it’s a declared module for initialising the script in the application. Here we used class with an export statement, and using constructor, we want to create the object wherever we need that specific module on the script application.

Example #2

Test.d.ts

declare module "examp" {
export interface examp {
demoid?: string;
demoid2?: string;
demoid3?: string;
demoid4?: string;
demoid5?: string;
demoid6?: string;
demoid7?: string;
demoid8?: string;
demoid9?: string;
demoid10?: string;
}
export function examp2(
demoid11: string,
): examp;
}
declare module "examps" {
export function first(xr: string): string;
export function second(...vars1: any[]): string;
export var yr: string;
}

Third.ts:

/// <reference path="test.d.ts"/>
import * as test from "examp";
let tests = "Welcome To My Domain";
console.log(tests);

Output:

TypeScript declare module 2

In the second example, we used the same declared module statement with the interface; instead of classes, we used an interface for initialising the variables with default data type like string. So that we can declare the values of any type, it depends upon the user requirement. We also referred to the declare module in the third. Is file with <reference path> tag, and also here we imported all the functions and variables which has been declared on the test.d.ts declare module file.

Example #3

Test.d.ts

declare module "first" {
class demo {
constructor()
}
export = demo
}

Third.ts

/// <reference path="test.d.ts"/>
import * as test1 from "./first";
let test1e= "Welcome To My Domain Its a Final Example";
console.log(test1e)
let test2e= "has asjv jcv jcv  jb j whdk jdb wjdg kdjbj";
console.log(test2e)
let test3e= "jhgs jhvsd jv jbdjvb kjw dwb dj kjdb kjdb";
console.log(test3e)
let test4e= "jwhg 73 g73 udgs g jwhqgw jw jh gwjhgwhjg jg";
console.log(test4e)
let test5e= "jhg hgas hj hj ashsas hjas hjsa j j hjasj hasjh hjbas j";
console.log(test5e)
let test6e= "jgs hv hj jvs hjgv gjvs gsvs vs j hgj j s jgwq jw jkw qbjw";
console.log(test6e)

Output:

Kripya

In the final example, the declaring module initialised the class and constructor for creating the object. And parallel, we can import the declare module script on the third.ts file like import * it denotes all the functions and methods added on the module. We can denote the path by using “./” these operator denotes the file path which has to be imported with the current script.

Rules and Regulations for declare module

  • Basically, the declare module will be saved with the .d.ts format; the rest of the files are in the .ts format only.
  • We can’t initialise the variable values on the declared module inside blocks.
  • The implementation also not accepted on the declared module.

Conclusion

In the conclusion part, the TypeScript declares module is one of the pre-defined codes for implementing the same workspace application. We needed the module name to be imported and utilised the functions and variables. The declare module is the index, and the beginning of the script accepts the only declaration the implementation is created with the next file like .ts, .js, etc.

Recommended Articles

We hope that this EDUCBA information on “TypeScript declare module” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript typeof
  2. TypeScript let
  3. What is TypeScript?
  4. TypeScript Versions

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW