2016-08-10 63 views
0

私はreactでtypescriptを使用しています。react-stickyというサードパーティ製のライブラリにタイピングを提供する必要があります。私はタイピングモジュールを公開したくないので、タイピングを書いて自分のコードベースに入れたいと思っていますが、困っています。ここでは、セットアップTypescript '宣言ファイルでモジュールが見つかりません'

App.tsx

/// <reference path="./typings/index.d.ts" /> 
/// <reference path="./typings/react-sticky.d.ts" /> 
import * as React from "react" 
import { Sticky, StickyContainer } from "react-sticky" 

const App: React.StatelessComponent<{}> =() => { 
    return <StickyContainer> 
     <Sticky> 
     <h1>Hello World</h1> 
     </Sticky> 
    </StickyContainer> 
} 

/typings/react-sticky.d.ts

/// <reference path="./modules/react/index.d.ts" /> 
import * as React from "react" 

declare module "react-sticky" { 

    export var StickyContainer: React.Component<{}, {}> 
    export interface StickyProps { 
     stickyStyle?: any 
     stickyClassName?: string 
     topOffset?: number 
     bottomOffset?: number 
     className?: string 
     style?: {} 
     onStickyStateChange?:() => void 
     isActive?: boolean 
    } 
    export var Sticky: React.Component<StickyProps, {}> 

} 

typings/index.d.ts

/// <reference path="modules/react-dom/index.d.ts" /> 
/// <reference path="modules/react/index.d.ts" /> 
/// <reference path="react-sticky.d.ts" /> 

私は取得していますエラーは、次

されます
App.tsx(3,41): error TS2307: Cannot find module 'react-sticky'. 

今、私はTypescriptを初めて使っています。ここにはおそらく複数のエラーがあります。

答えて

1

ほとんどあなたがいます。モジュール 'react-sticky'を宣言するのは初めてです(<>他の場所で宣言を補完する)、モジュール宣言の中にのインポートステートメントを置く必要があります。

// custom_typings/react-sticky.d.ts 
declare module "react-sticky" { 
    import * as React from "react" 
    var StickyContainer: React.ComponentClass<{}> 
    interface StickyProps { 
    stickyStyle?: any 
    stickyClassName?: string 
    topOffset?: number 
    bottomOffset?: number 
    className?: string 
    style?: {} 
    onStickyStateChange?:() => void 
    isActive?: boolean 
    } 
    var Sticky: React.ComponentClass<StickyProps> 
} 

楽しい事実:

  • あなたは///の参照を追加する必要はありません、あなたの宣言ファイルにエクスポートを追加する必要はありませんあなたの宣言ファイルは、次のようなものを見ることができますプロジェクトのルートに宣言ファイルを置いていない限り、どこでも可能です。
関連する問題