Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to create external file classes and use them

ANSWERED

So I'm a bit new to Typescript dev, and struggling a bit with defining and using class files from other .ts files.

I have a app that was working well, but didn't find time to work on it during the year, but now that I have some again and wanted to continue, I was greeted by an error when attempting to build. The error was

TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

The lines responsible for this in the index.ts file are:

import { Engine } from "./src/tamaController";

let engine = new Engine();

And the snippet from the file i'm attempting to import was:

export function Engine() {
    // code here..
}

At first, I had the .ts extension on my import statement, which resulted in my first error when returning to dev on this project. Apparently, you can no longer import files and supply the .ts extension?

I've tried to change the code in the tamaController.ts file to:

export class Engine {
      constructor() {
          //..
      }
  
      testFunction() {
          console.log("Test");
      }
}

But that failed because "Cannot find name 'console'." and I'm pretty sure for debugging I used to use console.log in my apps.

 

I've also noticed, if I try to do: 

import clock from "clock";

At the top of my tamaController.ts file containing my class, it fails with the same error as console above:

TS2307: Cannot find module 'clock'.

Not sure what would be the correct way to work with objects in TS for Fitbit, or if what I tried with the export class is correct, you just need to require the API imports from Fitbit differently in those files.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Ok, so this is not perfect, but I got it to work at least

In my index.ts I have

import clock from "clock";

import { Engine } from "./src/tamaController";

let engine = new Engine(clock, console);
engine.testFunction();

And in my tamaController.ts I have:

export class Engine {
      console: any
  
      constructor(clock: any, console: any) {
          this.console = console;
        
          clock.granularity = "minutes";
      }
  
      testFunction() {
          this.console.log("Test");
      }
}

The clock part is just there so that it doesn't build empty and error, but hopefully acts as a example for others

I'm marking this as the solution, but still not happy passing all the imports like that since my app will have many imports...

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

Just a small update, to try and get past the unable to import part, I tried the following

In my index.ts

import clock from "clock";
import { Engine } from "./src/tamaController";

let engine = new Engine(clock);

And in my tamaController.ts

export class Engine {
clock: Object

constructor(clock: Object) {
this.clock = clock;
}

testFunction() {
this.clock.granularity = "minutes";
}
}

But this fails with

TS2339: Property 'granularity' does not exist on type 'Object'.

So my idea of passing it seems to not work either 😞

 

Best Answer
0 Votes

Ok, so this is not perfect, but I got it to work at least

In my index.ts I have

import clock from "clock";

import { Engine } from "./src/tamaController";

let engine = new Engine(clock, console);
engine.testFunction();

And in my tamaController.ts I have:

export class Engine {
      console: any
  
      constructor(clock: any, console: any) {
          this.console = console;
        
          clock.granularity = "minutes";
      }
  
      testFunction() {
          this.console.log("Test");
      }
}

The clock part is just there so that it doesn't build empty and error, but hopefully acts as a example for others

I'm marking this as the solution, but still not happy passing all the imports like that since my app will have many imports...

Best Answer
0 Votes