Typescript空错误(ReSharper):无法将类型“ any”转换为接口类型

时间:2018-07-16 12:53:24

标签: typescript resharper typescript2.0

我在一些较旧的代码上遇到了奇怪的ReSharper(2017.3.3)Intellisense错误,我最近才注意到。在这段时间内,我们已从Visual Studio 2015升级到2017(这也意味着升级到TypeScript 2.0),并且我也开始使用ReSharper。我注意到禁用ReSharper可以解决问题,所以我想这与此有关,但这是否是一个bug,还是我误会了某些东西?

interface IMyInterface {
    Field1?: string;
    Field2?: number;
}

// ...

var myObj: IMyInterface;
myObj = null; //<--- intellisense error: Cannot convert type 'any' to type 'IMyInterface'. Type 'any' has no properties in common with type 'IMyInterface'

我知道TypeScript引入了一些可为null的类型功能(在此处记录:https://blog.mariusschulz.com/2016/09/27/typescript-2-0-non-nullable-types),但该错误看起来是错误的(null是“ any”)。有一种变通方法,似乎没有必要:

myObj = <IMyInterface>null; //workaround?

...但是那不对吗?

对于真正的类(例如内置字符串或我自己的MyClass类等),这不会发生,所以我认为它只是接口实现的。

在ReSharper中遇到问题?还是比我认为的要聪明?

0 个答案:

没有答案