w3resource

TypeScript Default Export

TypeScript Modules and Namespaces : Exercise-4 with Solution

Write a TypeScript module that exports a default function or class. Import the default export into another TypeScript file and use it to perform a task or create an instance.

Sample Solution:

TypeScript Code:

main.ts

import greet from './test_module';

// Use the default exported function
console.log(greet('Francis'));  

test_module.ts

// Export a default function
export default function greet(name: string): string {
  return `Hello, ${name}!`;
}

Output:

Hello, Francis!

TypeScript Editor:

See the Pen TypeScript by w3resource (@w3resource) on CodePen.


Previous: TypeScript Named Exports
Next: TypeScript Circular Dependencies.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.