Câu hỏi phỏng vấn Typescript
Câu hỏi

How can you allow classes defined in a module to be accessible outside of the module?

Câu trả lời

To allow classes defined in a TypeScript module to be accessible outside of the module, you need to use the export keyword. This keyword makes the classes, along with any other variables, functions, or interfaces you choose, accessible from other modules by explicitly exporting them.

Here's a step-by-step explanation and example:

Step 1: Define and Export the Class

In your TypeScript module, define the class you want to be accessible outside the module and prefix it with the export keyword. This makes the class public, allowing it to be imported by other modules.

Example:

typescript Copy
// file: University.ts
export class Student {
    constructor(public name: string, public age: number) { }
}

Step 2: Import the Class in Another Module

Once the class is exported, you can import it into another TypeScript file using the import statement. Specify the name of the class and the relative path to the module file where the class is defined.

Example:

typescript Copy
// file: App.ts
import { Student } from './University';

let student = new Student("John Doe", 22);
console.log(student);

Additional Notes:

  • If you want to export multiple classes or items from a s...
middle

middle

Gợi ý câu hỏi phỏng vấn

entry

What is TypeScript and why do we need it?

expert

How to make a union type from a type alias or interface properties in TypeScript?

senior

Explain what is never datatype in TypeScript?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào