Modules Flashcards
How is a module defined in Typescript?
In TypeScript, just as in ECMAScript 2015, any file containing a top-level import
or export
is considered a module.
“How JavaScript Modules are Defined” (typescriptlang.org). Retrieved May 22, 2023.
What is the difference between a module and a script?
Modules are executed within their own scope, not in the global scope. This means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms.
Scripts aka non-modules are executed in the global scope. File variables and types are declared to be in the shared global scope, and it’s assumed that you’ll either use the outFile compiler option to join multiple input files into one output file, or use multiple <script>
tags in your HTML to load these files (in the correct order!).
“Non-modules” (typescriptlang.org). Retrieved May 22, 2023.
How can you convert a script into a module?
If you have a file that doesn’t currently have any imports or exports, but you want to be treated as a module, add the line:
export {};
which will change the file to be a module exporting nothing. This syntax works regardless of your module target.
“Non-modules” (typescriptlang.org). Retrieved May 22, 2023.
What are the 3 main things to consider when writting a module in TypeScript?
There are three main things to consider when writing module-based code in TypeScript:
- Syntax: What syntax do I want to use to import and export things?
- Module Resolution: What is the relationship between module names (or paths) and files on disk?
- Module Output Target: What should my emitted JavaScript module look like?
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
What is the purpose of export
keyword in TypeScript?
The export
keyword in TypeScript is used to make properties, methods, or module declarations available to be imported into other modules.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
How do you import a module in TypeScript?
You can import a module in TypeScript using the import
keyword followed by the path to the module.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
Can you import a subset of a module in TypeScript?
Yes, you can. Use curly brackets { }
to specify the parts you want to import.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
How do you alias module components during import in TypeScript?
You can use the as
keyword to give a new name to imported entities.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
How to export everything from a module in TypeScript?
You can use export * from 'module'
to export everything from a module.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
What does the default
keyword mean in TypeScript modules?
The default
keyword can be used to specify the default export from a module. This can then be imported without using curly braces.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
How do you import a default
export from a TypeScript module?
Default exports can be imported without curly braces. For example, import myDefault from 'my-module'
.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
What’s the difference between import * as
and import {}
from in TypeScript?
-
import * as
imports the whole module and you can access its exports via dot notation. -
import {} from
allows you to import specific named exports directly.
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
What is the syntax for importing a module in TypeScript?
import moduleDefault, { ModuleComponent } from 'path/to/module';
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
What is the syntax for importing everything as an alias from a module?
import * as ModuleAlias from 'path/to/module';
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.
How do you export a component, function or value in TypeScript?
export const component = value;
“Modules in TypeScript” (typescriptlang.org). Retrieved May 22, 2023.