无法转换对象以键入间隔打字稿

时间:2019-07-31 14:22:44

标签: javascript angular typescript

我有一个对象a,里面有很多属性。在编辑时,我保存了这些属性,其中之一是interval。稍后我将其分配给其他属性(typedObject)一切正常。然后,我单击“编辑”,并且不更改interval,然后尝试将其分配给typedObject,但是它以某种方式表明它是Object而不是Interval的类型。我尝试将其转换为Interval类型,例如:

const typedObject: Interval = a.interval as Interval;

还尝试了:const typedObject: Interval = <Interval>a.interval;

但是它typedObject仍然是Object。为什么会这样呢?

我需要使用仅适用于类型Interval的函数,调用typedObject.getRange();时会出现异常

Interval是一门课程。

我收到错误消息: Uncaught TypeError: a.interval.getRange is not a function

如果我这样做,它将起作用:

        let typedObject = new IntervalRange();
        typedObject.day = a.interval.day;
        typedObject.month = a.interval.month;
        typedObject.quarter = a.interval.quarter;
        typedObject.timezone = a.interval.timezone;
      
        const from = <Interval>typedObject.getRange().from;
        const to = <Interval>typedObject.getRange().to;

但是一一分配typedObject.quarter = a.interval.quarter;看起来并不干净

0 个答案:

没有答案
相关问题