2016-08-10 4 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'를 처음 선언 할 때 (<> 다른 곳에서 선언을 추가하는 경우), 모듈 선언 안에 import 문을 넣어야합니다.

// 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> 
} 

재미있는 사실 :

  • 당신이 /// 참조를 추가 할 필요가 없습니다 당신이 당신의 선언 파일에 수출을 추가 할 필요가 없습니다 귀하의 선언 파일은 다음과 같은 것을 볼 수 있었다 프로젝트의 루트에 선언 파일을 두지 않는 한 어디서나 사용할 수 있습니다.
관련 문제