Methods of export/import

 avatar
c0dezer019
typescript
2 months ago
1.3 kB
5
Indexable
Never
// Default exports
export default { Car, Truck };
export default [Car, Truck];
export default Car;
export default function Car() {/* ... */};
export default const car = "Chevy";

// Exporting computed values directly.
export default 1 + 1;

// Conditional exporting
export default obj1 || obj2; //obj1 if obj1 is truthy, obj2 if obj1 is falsy
export default obj1 && obj2; //obj1 if obj1 is falsy, obj2 if obj1 is truthy.

// Named
export const lettuce = "Green stuff";
export let variable: string;
export function Car() {/* ... */};

/* Exporting from an index file placed in a folder will
 * allow to import from a single location instead of
 * having multiple import statements. This also helps
 * with organization and makes your base more modular.
 * This is called re-exporting.
 */
export { lettuce, /* ... */ } from './vegetable.ts'; //extension is optional

// Wildcard exporting will export
export * from './constants.ts';
export * as constants from './constants.ts';

// Re-exporting a default export.
export { default as Vehicle } from './Vehicle.tsx';
export { default, /* ... */} from './Vehicle.tsx';

// Importing

import * as constants from './constants.ts';
import iObject from './interfaces.ts';
import { Vehicle as Automotive, /* ... */ } from './Vehicle.tsx';
import { Vehicle, /* ... */ } from '/Vehicle.tsx';
Leave a Comment