Modules Flashcards

1
Q

How is a module defined in Typescript?

A

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a module and a script?

A

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!).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you convert a script into a module?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 3 main things to consider when writting a module in TypeScript?

A

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?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of export keyword in TypeScript?

A

The export keyword in TypeScript is used to make properties, methods, or module declarations available to be imported into other modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you import a module in TypeScript?

A

You can import a module in TypeScript using the import keyword followed by the path to the module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you import a subset of a module in TypeScript?

A

Yes, you can. Use curly brackets { } to specify the parts you want to import.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you alias module components during import in TypeScript?

A

You can use the as keyword to give a new name to imported entities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to export everything from a module in TypeScript?

A

You can use export * from 'module' to export everything from a module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the default keyword mean in TypeScript modules?

A

The default keyword can be used to specify the default export from a module. This can then be imported without using curly braces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you import a default export from a TypeScript module?

A

Default exports can be imported without curly braces. For example, import myDefault from 'my-module'.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the difference between import * as and import {} from in TypeScript?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax for importing a module in TypeScript?

A
import moduleDefault, { ModuleComponent } from 'path/to/module';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the syntax for importing everything as an alias from a module?

A
import * as ModuleAlias from 'path/to/module';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you export a component, function or value in TypeScript?

A
export const component = value;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you re-export something imported from another module?

A
export { importedComponent } from 'path/to/module';
17
Q

How do you export default in TypeScript?

A
export default Component;
18
Q

How do you import a default export in TypeScript?

A
import Component from 'path/to/module';
19
Q

What is the syntax for importing a type in TypeScript?

A
import { TypeName } from 'path/to/module';

Or

import type { TypeName } from 'path/to/module';
20
Q

How can you export and import an interface in TypeScript?

A

Export:

export interface InterfaceName {...} 

Import:

import { InterfaceName } from 'path/to/module';
21
Q

How can you export and import a type in TypeScript?

A

TypeScript has extended the import syntax with two concepts for declaring an import of a type:

  • import type - Which is an import statement which can only import types:
// @filename: animal.ts
export type Cat = { breed: string; yearOfBirth: number };
'createCatName' cannot be used as a value because it was imported using 'import type'.
export type Dog = { breeds: string[]; yearOfBirth: number };
export const createCatName = () => "fluffy";
 
// @filename: valid.ts
import type { Cat, Dog } from "./animal.js";
export type Animals = Cat | Dog;
 
// @filename: app.ts
import type { createCatName } from "./animal.js";
const name = createCatName();
  • Inline type imports - TypeScript 4.5 also allows for individual imports to be prefixed with type to indicate that the imported reference is a type:
// @filename: app.ts
import { createCatName, type Cat, type Dog } from "./animal.js";
 
export type Animals = Cat | Dog;
const name = createCatName();

Together these allow a non-TypeScript transpiler like Babel, swc or esbuild to know what imports can be safely removed.