Import javascript in TypeScript

0

Good morning! I have the following problem:

I am starting in typescript and to start working I will be going through some of my javascript of a project that I have running.

For this I started with my Main.js changing the name to .ts and moving it to a folder where I will have all the source typeScript (within the same project)

Within my main file I make references to other classes, I put here part of the code:

var SiteController=function() {
    this.eventManager = new EventManager(this);
}   

The problem is that typescript says it can not find EventManager.

What I do not know is if I do not have to do some kind of import of all the javascript I have in my other folder. The error that throws me is the following:

  

file:   'file: ///d%3A/_PROYECTOS/turner/TURNER_WB_TBBT_TriviaFan/deploy/js/typeScript/SiteController.ts'   severity: 'Error' message: 'Can not find name' EventManager '.' source:   'ts'

I imagine it should be something like

  

import '../ js / bin /';

but the truth is that I do not know how I have to do to import all my javascript classes either from my own or from other libraries and not have this error

the folder structure I handle is:

deploy
deploy/js
deploy/js/typeScript
deploy/js/bin

Within typescript are the .ts files and inside bin there are folders with third-party libraries or own classes

since thank you very much

    
asked by Pablo 05.05.2017 в 14:07
source

2 answers

1

You have to import the class EventManager :

import {EventManager} from '../RUTA';

If you wanted to import all the classes of a namespace it should be done like this:

MisClases.ts

export namespace clase{
    export class EventManager {
        //tu codigo
    }
    export class OtraClase { 
        //tu codigo
    }
}

And to import them

import * as misClases from './MisClases';

To use them

let eventManager = misClases.clase.EventManager();
    
answered by 05.05.2017 / 14:10
source
0

Thanks for helping, part already served me but I can not convince the way to import them from the namespace, the reason is that I already have many files of classes with their code in different files and my idea is to go passing the project little by little to typescript, if I have to add or modify all the calls of the classes I'm going to have to refactor a lot of code, unless I'm not understanding the mechanics well to do it.

so for now I'm doing it like this: import {EventManager} from '../ bin / EventManager.js';

The problem I have is that I'm throwing this error:

file: 'file:///d%3A/_PROYECTOS/turner/TURNER_WB_TBBT_TriviaFan/deploy/js/typeScript/SiteController.ts'
severity: 'Error'
message: 'Module '../bin/EventManager.js' was resolved to 'd:/_PROYECTOS/tur/T_WB_TBBT_TriviaFan/deploy/js/bin/EventManager.js', but '--allowJs' is not set.'
at: '1,28'
source: 'ts'
    
answered by 05.05.2017 в 14:32