IntelliJ IDEA 报告未解决的变量 TypeScript 错误,但不允许 ':any'

IntelliJ IDEA reporting unresolved variable TypeScript errors, but won't allow ':any'

我在 Angular 2 应用程序中有以下 TypeScript 代码。

   constructor(private windows:WindowService, private http:Http) {
        http.get('config.json')
            .map(res => res.json())
            .subscribe(config => {
                this.oAuthCallbackUrl = config.callbackUrl; // here
                this.oAuthTokenUrl = config.implicitGrantUrl; // here
                this.oAuthTokenUrl = this.oAuthTokenUrl
                    .replace('__callbackUrl__', config.callbackUrl) // here
                    .replace('__clientId__', config.clientId) // here
                    .replace('__scopes__', config.scopes); // here
                this.oAuthUserUrl = config.userInfoUrl; // here
                this.oAuthUserNameField = config.userInfoNameField; // here
            })
    }

每个我有 // here 的地方我都会从 IDE 中得到一个错误,说我有一个 'unresolved variable',例如 unresolved variable callbackUrl.

问题是这个配置对象是应用程序获取 JSON 文件的结果,我不知道如何提前定义它的类型。

我想我也许可以更改订阅行以显示 .subscribe(config:any => { 或其他内容,但这没有用。

代码编译正常。一切都可以在浏览器中(以及通过 Webpack)正常运行。我只是想摆脱 IDE 中的错误,而不必添加 7 //noinspection TypeScriptUnresolvedVariable 评论来压制它们。

正如@DCoder 在上面的评论中提到的,解决方案是将 config 参数包裹在括号中,然后将类型添加到其中。

constructor(private windows:WindowService, private http:Http) {
    http.get('config.json')
        .map(res => res.json())
        .subscribe((config:any) => {  //               <-- WORKS NOW!
            this.oAuthCallbackUrl = config.callbackUrl;
            this.oAuthTokenUrl = config.implicitGrantUrl; 
            this.oAuthTokenUrl = this.oAuthTokenUrl
                .replace('__callbackUrl__', config.callbackUrl) 
                .replace('__clientId__', config.clientId) 
                .replace('__scopes__', config.scopes); 
            this.oAuthUserUrl = config.userInfoUrl;
            this.oAuthUserNameField = config.userInfoNameField; 
        })
}

由于缺少括号,我无法在 config 上添加任何类型的输入信息。