TypeScript Exporting Classes

TypeScript Modules and Namespaces : Exercise-2 with Solution

Write a TypeScript module that exports a class called Student with properties name and age. Import the Student class in another TypeScript file, create instances of Student, and display their information.

Sample Solution:

TypeScript Code:


import {Student} from './student';

// Create instances of Student
const student1 = new Student('Doris Mauri', 18);
const student2 = new Student('Joel Darinka', 20);

// Display student information


export class Student {
  constructor(public name: string, public age: number) {}

  displayInfo() {
    console.log(`Name: ${this.name}, Age: ${this.age}`);


The above code imports the "Student" class from the "student.ts" module, creates two instances of 'Student', and displays their information in the console.


Name: Doris Mauri, Age: 18
Name: Joel Darinka, Age: 20

TypeScript Editor:

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

Previous: TypeScript Exporting Functions
Next: TypeScript Named Exports.

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.