Domanda

interface IModal {
    widgetNames: KnockoutObservableArray<string>;
    widgets: KnockoutObservableArray<IWidget>;
}

one web.d.ts file where i declare module

declare module "modules/dialog/modal" {
    var theModal: IModal;
    export = theModal;
}

class modal implements IModal {
   widgetNames: KnockoutObservableArray<string>;
   widgets: KnockoutObservableArray<IWidget>;

 constructor() {
    this.widgetNames= ko.observable<string>(['widget1','widget2'])
 }
}
export = modal; 

Class index is where i want to import modal.ts file and create new object. my problem is that when import modal.ts then unable to create object. Typescript not compiler index.ts file here "new modal()"

import modal = require('modules/dialog/modal');
class index{
constructor(){
  var_modal = new modal();//problem here, unable to create modal. not compiled by typescript compiler; i don't want to use singleton pattern;

 }
}
export = index;

solution is import that: this work fine

import modal = require('./modal');//modal path
class index{
constructor(){
  var_modal = new modal();

 }
}
export = index

this work fine, but if i want to change ('./modal') path: "../../widgets/personInfo/viewmodel" after rebuild solution, visual studio shot an erros:

Unable to resolve external module "../../widgets/personInfo/viewmodel" Build: Module cannot be aliased to a non-module type. Invalid 'new' expression

È stato utile?

Soluzione

you shouldn't need :

declare module "modules/dialog/modal" {
    var theModal: IModal;
    export = theModal;
}

if you have a file fooModal.ts as :

class modal implements IModal {
   widgetNames: KnockoutObservableArray<string>;
   widgets: KnockoutObservableArray<IWidget>;

 constructor() {
    this.widgetNames= ko.observable<string>(['widget1','widget2'])
 }
}
export = modal; 

You can simply do:

import modal = require('./fooModal');
class index{
constructor(){
  var_modal = new modal();//problem here, unable to create modal. not compiled; i don't want to use singleton pattern;

 }
}
export = index;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top