简体   繁体   English

为 read-more-react 创建 index.d.ts npm package

[英]create index.d.ts for read-more-react npm package

I have a typescript project,我有一个 typescript 项目,

I am trying to import read-more-react but beacuse it doesn't have a @type defined for it, I need to write the index.d.ts file myself (place it under @type/read-more-react),我正在尝试导入read-more-react但因为它没有为它定义@type,我需要自己编写 index.d.ts 文件(将其放在@type/read-more-react 下),

I have tried this:我试过这个:

declare module 'read-more-react' {

    import React from 'react';

    declare const ReadMoreReact:  React.FunctionComponent<ReadMoreReactProps>;

}

interface ReadMoreReactProps {
    text: string
    min: number
    ideal: number
    max: number
    readMoreText: string
}

but it doesn't seems to work,但它似乎不起作用,

can anyone help me in how to successfully implement the index.d.ts file, what am I missing?谁能帮助我如何成功实现 index.d.ts 文件,我错过了什么?

Inside your tsconfig.json file, you need to include your index.d.ts file.在您的 tsconfig.json 文件中,您需要包含您的index.d.ts文件。 You can do so by adding the following:您可以通过添加以下内容来做到这一点:

{
  "include": [
    "path/to/your/index.d.ts",
  ]
} 

Also use this code instead:也使用此代码:

declare module 'read-more-react' {
    import React from 'react';

    interface ReadMoreReactProps {
        text: string
        min: number
        ideal: number
        max: number
        readMoreText: string
    }

    const ReadMoreReact: React.FC<ReadMoreReactProps>;

    export default ReadMoreReact;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 index.d.ts 文件未发布到 NPM - index.d.ts file not published to NPM 我们如何使用危险的 SetInnerHTML 和 read-more-react - npm? - How can we use dangerouslySetInnerHTML with read-more-react - npm? 组件无法从 React Native 中的 index.d.ts 文件自动导入类型 - Component fails to auto import types from index.d.ts file in React Native 是否为canonical-json提供index.d.ts文件? - Providing an index.d.ts file for canonical-json? 在index.d.ts构建错误中找不到名称“Record” - Cannot find name 'Record' in index.d.ts build error 在源代码中使用 `index.d.ts` 声明文件 - Consume `index.d.ts` declaration file in source code 用于角度库的DefinitelyTyped vs index.d.ts - DefinitelyTyped vs index.d.ts for angular library 如何拆分大型的typescript index.d.ts文件 - How to split large typescript index.d.ts file 包含`declare module 'graphql/error/GraphQLError' 的声明(.d.ts)文件; 在 angular 项目的 index.d.ts 中 - declaration (.d.ts) file containing `declare module 'graphql/error/GraphQLError'; in index.d.ts in an angular project 如何从 index.d.ts 文件手动导入一个类型,并告诉 VS Code 智能感知一个变量是那种类型? - How to manually import a type from an index.d.ts file, and tell VS Code intellisense that a variable is of that type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM