Pregunta

I am using typescript with durandal as a proof of concept and honestly they aren't gelling.

Most recently I'm trying to troubleshoot the following intellisense error:

"Cannot convert typeof calendarServiceImport to ICalendarService"

///<reference path="service/CalendarService.ts"/>
import calendarServiceImport = require("service/CalendarService");

var calendarService: ICalendarService;
calendarService = calendarServiceImport;

With Durandal, the import call is supposed to be a DI call and should be an instance, but typescript parser is thinking it should be a type instead.

Here's the code I'm trying to import:

/// <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;

I could get rid of the error, but then I would be introducing logical error, which's even worse, because then it'd be an application bug.

eg. replace the following line: calendarService = calendarServiceImport; with calendarService = new calendarServiceImport(); but makes no sense as I would be instantiating the object a 2nd time.

How can I get around this error?

¿Fue útil?

Solución

Since TypeScript and Durandal disagree about the semantics of require ("import this" vs "import a new instance of this"), you'll need to add a cast somewhere.

Option 1 -- cast at the consumption site:

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

var calendarService = <ICalendarService><any>calendarServiceImport;

Option 2 -- cast at the export site:

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;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top