Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
JavaScript doesnt recognize my custom class from typescript file

So right now I'm simply testing to see if I can create and store some info in a Javascript file using a custom TypeScript class object. Before testing I'm running the command 'tsc courses.ts' followed by 'node app.js' It says its working and running on the port I specified but when I go to the page it break and says there is a reference error when doing 'let aCourse = new CourseObject' CourseObject is not defined.

Any ideas on how I might fix this?

courses.ts

class CourseObject {
    courseNumber: number;       
    courseName: string;
    courseDescription: string;
    credits: number;
    preRequisites: string;
    
    constructor(courseNumber: number, courseName: string, courseDescription: string,
                credits: number, preRequisites: string){
        this.courseNumber       = courseNumber;     
        this.courseName         = courseName;
        this.courseDescription  = courseDescription;
        this.credits            = credits;
        this.preRequisites      = preRequisites;
    }
}

app.js

... omitted for brevity ...

response.writeHead(200, {"Content-Type": "text/html"});
            
            let courses = [];
            let aCourse = new CourseObject();

... omitted for brevity ...

EDIT FIXED ran tsc --init to generatea tsconfig.json file. module: commonjs and target es2016

then changed code to: courses.ts

export class CourseObject{ ... }

and app.js added

const CourseObject = require(PATH/courses.js).CourseObject

Thanks @CRice for the help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • Export your class:
export class CourseObject {...}

  • When importing into a .ts file:
import { CourseObject } from 'path/CourseObject'
  • Or when importing into a plain .js file, use require:
const CourseObject = require("./path/CourseObject.js").CourseObject
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda