简体   繁体   English

找不到类型定义模块

[英]Type definitions module not found

I have a React project, written in TypeScript, with the following file structure:我有一个 React 项目,用 TypeScript 编写,文件结构如下:

├── BoldedText.tsx
├── highlighter
│   ├── Highlighter.js
│   └── index.t.ts
├── App.tsx

My BoldedText component is using the Highlight component (through the type definition index.d.ts since the component is written in JavaScript).我的BoldedText 组件正在使用 Highlight 组件(通过类型定义 index.d.ts,因为该组件是用 JavaScript 编写的)。

import React from 'react' import Highlighter from "./highlighter/index";从“react”导入 React 从“./highlighter/index”导入荧光笔;

interface Props {
    boldedWords: string[]
    text: string
}

const Highlight : React.ComponentType<any> = (props) => (
    <strong className="highlighted-text">{props.children}</strong>
);

const BoldedText = (props: Props) => {
    const { boldedWords, text } = props
        return (
            <Highlighter
                searchWords={boldedWords}
                autoEscape={true}
                highlightTag={Highlight}
                textToHighlight={text}
            />
        )
}

and this is my type definition file (index.d.ts):这是我的类型定义文件(index.d.ts):

import * as React from "react";

export interface FindChunks {
  autoEscape?: boolean;
  caseSensitive?: boolean;
  sanitize?: (text: string) => string;
  searchWords: string[];
  textToHighlight: string;
}

export interface Chunk {
  start: number;
  end: number;
}

export interface HighlighterProps {
    /** The class name to be applied to an active match. Use along with activeIndex */
    activeClassName?: string;
    /** Specify the match index that should be actively highlighted. Use along with activeClassName */
    activeIndex?: number;
    /** The inline style to be applied to an active match. Use along with activeIndex */
    activeStyle?: React.CSSProperties;
    /** Escape characters in searchWords which are meaningful in regular expressions */
    autoEscape?: boolean;
    /** CSS class name applied to the outer/wrapper <span> */
    className?: string;
    /** Search should be case sensitive; defaults to false */
    caseSensitive?: boolean;
    /**
     * Use a custom function to search for matching chunks. This makes it possible to use arbitrary logic
     * when looking for matches. See the default findChunks function in highlight-words-core for signature.
     * Have a look at the custom findChunks example on how to use it.
     */
    findChunks?: (options: FindChunks) => Chunk[];
    /** CSS class name applied to highlighted text */
    highlightClassName?: string;
    /** Inline styles applied to highlighted text */
    highlightStyle?: React.CSSProperties;
    /**
     * Type of tag to wrap around highlighted matches; defaults to mark but can also be a React element
     * (class or functional)
     */
    highlightTag?: string | React.ComponentType<any>;
    /**
     * Process each search word and text to highlight before comparing (eg remove accents); signature
     * (text: string): string
     */
    sanitize?: (text: string) => string;
    /** Array of search words. The search terms are treated as RegExps unless autoEscape is set. */
    searchWords: string[];
    /** Text to highlight matches in */
    textToHighlight: string;
    /** CSS class name applied to unhighlighted text */
    unhighlightClassName?: string;
    /** Inline styles applied to unhighlighted text */
    unhighlightStyle?: React.CSSProperties;
    /** Allows to pass through any parameter to wrapped component */
    [index: string]: any;
}

declare class Highlighter extends React.Component<HighlighterProps> {}

export default Highlighter;

However, when running the application I get the following error:但是,在运行应用程序时,我收到以下错误:

./src/components/util/BoldedText.tsx
Module not found: Can't resolve './highlighter/index'

I have tried restructuring my files, but I still get module not found for index.我已经尝试重组我的文件,但我仍然找不到索引模块。

Is Webpack configured that it also looks for t.ts extensions? Webpack 是否配置为也查找 t.ts 扩展? Check your webpack and ensure that all needed extensions are resolved检查您的 webpack 并确保解决所有需要的扩展

resolve: {
    extensions: ['.js', '.ts']
}

And also ensure that your entry path is correct.并且还要确保你的进入路径是正确的。 Mostly the default is public/src/ This should be also configured in the webpack.通常默认是public/src/这也应该在 webpack 中配置。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM