0
0
Typescriptprogramming~5 mins

Declaration merging for namespaces in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is declaration merging in TypeScript namespaces?
Declaration merging is when multiple namespace declarations with the same name combine their contents into a single namespace.
Click to reveal answer
beginner
How does TypeScript handle multiple namespace declarations with the same name?
TypeScript merges all declarations with the same namespace name into one, combining their members like variables, functions, or interfaces.
Click to reveal answer
intermediate
Can you add functions and variables in separate namespace declarations and have them accessible together?
Yes, functions and variables declared in separate namespace blocks with the same name are merged and accessible as if declared in one namespace.
Click to reveal answer
advanced
What happens if two merged namespaces declare the same function name with different implementations?
TypeScript will merge the declarations, but the last implementation will overwrite previous ones, so only one function body is used.
Click to reveal answer
intermediate
Why is declaration merging useful in namespaces?
It allows splitting code into multiple parts or files while keeping related code grouped under one namespace, improving organization and modularity.
Click to reveal answer
What does declaration merging for namespaces allow you to do?
AMerge classes with namespaces
BCombine multiple namespace declarations with the same name into one
CAutomatically convert namespaces to classes
DPrevent namespaces from having the same name
If two namespaces with the same name declare different variables, what happens?
AAn error occurs due to duplicate declarations
BOnly the first namespace's variables are kept
CVariables from both namespaces are merged and accessible
DVariables are renamed automatically
Can declaration merging be used across multiple files?
AYes, namespaces with the same name in different files merge
BOnly if files are imported as modules
CNo, it only works within a single file
DOnly for classes, not namespaces
What happens if two merged namespaces declare a function with the same name but different code?
AThe functions are merged into one combined function
BTypeScript throws an error
CBoth functions exist separately
DThe last declared function overwrites the previous one
Why might you use declaration merging for namespaces?
ATo split related code into multiple parts but keep it under one namespace
BTo avoid using namespaces altogether
CTo automatically generate classes
DTo merge unrelated code into one file
Explain how declaration merging works for namespaces in TypeScript and why it is useful.
Think about how you can write parts of a namespace in different places and still access all members together.
You got /4 concepts.
    Describe what happens when two merged namespaces declare a function with the same name but different implementations.
    Consider which function code TypeScript uses when names collide.
    You got /4 concepts.