使用声明文件打包'无法找到模块'

时间:2016-08-10 11:44:42

标签: typescript

我正在使用typescript with react,需要为名为react-sticky的第三方库提供输入。我不想发布一个打字模块,我只想写这些打字并将它们包含在我的代码库中,但我遇到了麻烦。这是设置

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 个答案:

答案 0 :(得分:3)

你几乎就在那里。由于这是您第一次声明模块“反应粘性”,(&lt;&gt;在其他地方增加声明),您必须将导入声明放在您的模块声明。您的声明文件可能如下所示:

 getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();

有趣的事实:

  • 您不需要在声明文件中添加导出
  • 您不需要在任何地方添加// 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> } 引用,只要您不将声明文件放在项目的根目录中。