Named vs. Default Imports in TypeScript
TypeScript provides two different ways to import functionality from a module: named imports and default imports. While both types of imports allow you to use code from other modules in your application, they have some key differences in terms of syntax and behavior.
Named Imports
A named import allows you to selectively import specific functionality from a module using a named identifier. You can import multiple named exports from a module by separating them with commas inside a set of curly braces {}
.
|
|
Default Imports
A default import allows you to import a single default export from a module using any identifier you choose. You can only have one default export per module.
|
|
In this example, we use a default import to import the default export from the math module. Since the default export is an object with a sum
property that contains the sum
function, we can access the sum
function using dot notation.
Exporting Named and Default Exports
A single named export: sum
|
|
A single default export:
|
|
Cheers! 🍺