0
0
Typescriptprogramming~5 mins

Module augmentation syntax in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is module augmentation in TypeScript?
Module augmentation is a way to add new members or modify existing ones in an already declared module without changing the original source code.
Click to reveal answer
beginner
How do you start a module augmentation block in TypeScript?
You use the declare module 'module-name' syntax to open a block where you can add or change module contents.
Click to reveal answer
intermediate
Can you augment a module by adding new functions? Give an example.
Yes. For example, to add a new function <code>newFunc</code> to module <code>myModule</code>:
<pre>declare module 'myModule' {
  export function newFunc(): void;
}</pre>
Click to reveal answer
intermediate
What happens if you augment a module with a conflicting type or member?
TypeScript will merge compatible declarations, but if there is a conflict (like different types for the same member), it will cause a type error.
Click to reveal answer
beginner
Why use module augmentation instead of editing the original module?
Because the original module might be from a library or external source you can't or shouldn't change. Augmentation lets you safely extend it.
Click to reveal answer
Which keyword starts a module augmentation block in TypeScript?
Aextend
Bmodule
Cdeclare
Daugment
What is the main purpose of module augmentation?
ATo delete members from a module
BTo create a new module
CTo rename a module
DTo add or modify members in an existing module
If you add a new function in module augmentation, what must you do?
AOnly declare the function signature in the augmentation block
BDelete the original function
CImplement the function inside the augmentation block
DNothing, it works automatically
What happens if two augmentations declare the same member with different types?
ATypeScript throws a type error
BTypeScript merges them without error
CThe last declaration overwrites the first silently
DThe compiler ignores the conflict
Which scenario is best suited for module augmentation?
AChanging core library code directly
BExtending third-party library types safely
CCreating brand new modules
DDeleting unused code
Explain how to use module augmentation syntax to add a new function to an existing module.
Think about how you tell TypeScript about new parts without changing original code.
You got /3 concepts.
    Describe what happens when conflicting types are declared in module augmentation and how to avoid it.
    Conflicts cause errors; be careful to match or extend types properly.
    You got /3 concepts.