2016-12-26 74 views
1

我有以下類型聲明爲r-dom寫成:錯誤與打字稿定製類型聲明

/// <reference types="react" /> 

declare module 'r-dom' { 

    interface IRDOMFacade extends React.ReactDOM { 
     (component: React.Component<any, any>, properties?: Object, children?: React.ReactElement<any>|Array<React.ReactElement<any>>): React.ReactElement<any> 
    } 

    var r: IRDOMFacade 

    export = r 
} 

試樣成分:

import * as React from 'react' 
import * as r from 'r-dom' 

export default class Application extends React.Component<{},{}> { 

    render() { 
     return r.h1('Hello World') 
    } 

} 

上述部件沒有類型的錯誤。但是,當我嘗試使用R作爲一個功能,我得到錯誤類型:

r(Application) 

error TS2345: Argument of type 'typeof Application' is not assignable to parameter of type 'Component'. Property 'setState' is missing in type 'typeof Application'.

我很好奇,什麼是錯在這裏。

回答

0

發生這種情況是因爲r的第一個參數是具有簽名React.Component<any, any>而非React.Component<any, any>的實例的類/構造函數。

所以切換到跟隨修正錯誤:

interface IRDOMFacade extends React.ReactDOM { 
    (
     component: new (...args: any[]) => React.Component<any, any>, 
     properties?: Object, 
     children?: React.ReactElement<any>|Array<React.ReactElement<any>> 
    ): React.ReactElement<any> 
} 

我會保持這種開放幾天的情況下,有一個人更地道的解決方案。