Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing a class with Typescript

I'm trying to import a class called review I have from another typescript file called review.ts into my review-service.ts file however I don't know why I'm getting an error but I don't think the review.ts file I have is being imported properly into the review-service.ts file.

The image below is the file with the error which outlines the path of the file I'm trying to import from and the file structure of the app

Screenshot 1

Also in another screenshot I have it shows the "module" for the path is coming up as *. I have no idea why this is an I've tried many different paths to try get this to work.

Screenshot 2

The constents of review.ts are as follows

  //review.ts
  export class Review {
    _id: number;
    description: string;
    isComplete: boolean;
  }
like image 702
Smurfffy Avatar asked Mar 21 '17 16:03

Smurfffy


People also ask

How do you use export classes in TypeScript?

Use named exports to export multiple classes in TypeScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a file.

How do I import a function in TypeScript?

To import a function from another file in TypeScript: Export the function from file A , e.g. export function sum() {} . Import the function in file B as import { sum } from './another-file' . Use the imported function in file B .

How can we access a class of module from outside in TypeScript?

In typescript by using an import statement, a module can be utilized in another module. any variables, classes, methods, or other objects declared in a module are not accessible outside of it. The keyword “export” may be used to construct a module, and the term “import” can be used to import it into another module.

Can you import interfaces in TypeScript?

TypeScript uses the concept of modules, in the same way that JavaScript does. In order to be able to import an interface from a different file, it has to be exported using a named or default export.


1 Answers

import {Review} from '../app/review'

instead of

import {Review} from '../app/review.ts'

That's all :)

like image 128
Mateusz Szymik Avatar answered Oct 19 '22 06:10

Mateusz Szymik