Javascript 如何在不触发intellisense的情况下导入typescript中的对象?

Javascript 如何在不触发intellisense的情况下导入typescript中的对象?,javascript,requirejs,typescript,durandal,durandal-2.0,Javascript,Requirejs,Typescript,Durandal,Durandal 2.0,我正在使用durandal的typescript作为概念证明,老实说,它们没有胶化 最近,我尝试对以下intellisense错误进行故障排除: “无法将calendarServiceImport的类型转换为ICalendarService” /// 导入calendarServiceImport=require(“服务/日历服务”); var calendarService:ICalendarService; calendarService=calendarServiceImport; 对于

我正在使用durandal的typescript作为概念证明,老实说,它们没有胶化

最近,我尝试对以下intellisense错误进行故障排除:

“无法将calendarServiceImport的类型转换为ICalendarService”

///
导入calendarServiceImport=require(“服务/日历服务”);
var calendarService:ICalendarService;
calendarService=calendarServiceImport;
对于Durandal,导入调用应该是一个DI调用,并且应该是一个实例,但是typescript解析器认为它应该是一个类型

以下是我尝试导入的代码:

/// <reference path="../contracts/ICalendarService.ts"/>
/// <reference path="../../configure.d.ts"/>
import configuration = require('viewmodels/configure')

class CalendarService implements ICalendarService {
    private NumberOfDaysToSync : number;

    constructor() {
    }

    getCalendarNames(): string[] {            
        return ["My Calendar","My Other Calendar"];
    }

    pullLatestSchedule(calendarName : string) {

    }
}

export = CalendarService;
//
/// 
导入配置=需要('viewmodels/configure')
类CalendarService实现ICalendarService{
private NumberOfDaysToSync:编号;
构造函数(){
}
getCalendarNames():字符串[]{
返回[“我的日历”、“我的其他日历”];
}
pullLatestSchedule(日历名称:字符串){
}
}
导出=日历服务;
我可以消除这个错误,但接下来我会引入逻辑错误,更糟糕的是,因为这会是一个应用程序错误

例如,替换以下行:
calendarService=calendarServiceImport带有
calendarService=new calendarServiceImport()但没有意义,因为我将第二次实例化该对象


如何避免此错误?

由于TypeScript和Durandal对
require
(“导入此”与“导入此的新实例”)的语义存在分歧,因此您需要在某个位置添加强制转换

选项1——在消费现场浇筑:

import calendarServiceImport = require("service/CalendarService");

var calendarService = <ICalendarService><any>calendarServiceImport;
import calendarServiceImport=require(“服务/日历服务”);
var calendarService=calendarServiceImport;
选项2--在导出站点进行强制转换:

import configuration = require('viewmodels/configure')

class CalendarService implements ICalendarService {
    private NumberOfDaysToSync : number;

    constructor() {
    }

    getCalendarNames(): string[] {            
        return ["My Calendar","My Other Calendar"];
    }

    pullLatestSchedule(calendarName : string) {

    }
}

var instance = <ICalendarService><any>CalendarService;    
export = instance;
import configuration=require('viewmodels/configure')
类CalendarService实现ICalendarService{
private NumberOfDaysToSync:编号;
构造函数(){
}
getCalendarNames():字符串[]{
返回[“我的日历”、“我的其他日历”];
}
pullLatestSchedule(日历名称:字符串){
}
}
var实例=日历服务;
导出=实例;

先生,您真是太棒了!TYVM!!!我选择了更晚的解决方案,始终在生产现场整合复杂性。-更具凝聚力。
import configuration = require('viewmodels/configure')

class CalendarService implements ICalendarService {
    private NumberOfDaysToSync : number;

    constructor() {
    }

    getCalendarNames(): string[] {            
        return ["My Calendar","My Other Calendar"];
    }

    pullLatestSchedule(calendarName : string) {

    }
}

var instance = <ICalendarService><any>CalendarService;    
export = instance;