简体   繁体   English

错误TS2339:类型“ TestPage”上不存在属性“ props”

[英]error TS2339: Property 'props' does not exist on type 'TestPage'

I'm using typescript abstract class as a base class of my layout component in React, the usage of that base class looks something like this: 我在React中使用Typescript抽象类作为布局组件的基类,该基类的用法如下所示:

import { IPageLayoutActions, IPageLayoutLocalState, IPageLayoutProps, PageLayoutNoSidebar } from 'pg-face/dist/layouts/PageLayoutNoSidebar';

export interface ITestPageProps extends IPageLayoutProps
{
    loremIpsum: string;
}
export interface ITestPageActions extends IPageLayoutActions {}
interface ITestPageLocalState extends IPageLayoutLocalState {}

export
class TestPage
extends PageLayoutNoSidebar<ITestPageProps && ITestPageActions, ITestPageLocalState>
{
    renderPageContent() {
        return <span>{ this.props.loremIpsum }</span>;
    }
}

The PageLayoutNoSidebar component actually extends another class called PageLayout, which is a base class for all layouts in my application. PageLayoutNoSidebar组件实际上扩展了另一个名为PageLayout的类,它是应用程序中所有布局的基类。 It works when everything is inside one project. 当所有内容都在一个项目中时,它就可以工作。

Now I wanted to move all these layout files into separate npm module to be reusable across all my applications (I'm just testing it so I'm loading the package from local directory). 现在,我想将所有这些布局文件移动到单独的npm模块中,以在所有应用程序中重复使用(我只是对其进行测试,所以要从本地目录加载程序包)。 So I created a package, transpiled the typecscript code into js and definition files using "tsc -d -p ./". 因此,我创建了一个程序包,并使用“ tsc -d -p ./”将typecscript代码转换为js和定义文件。 These files are stored in the dist dir of the package. 这些文件存储在软件包的dist目录中。 Then I updated the package in my project dir. 然后,我在项目目录中更新了软件包。

After that, when I try to use this class in my project, I got 之后,当我尝试在项目中使用此类时,

error TS2339: Property 'props' does not exist on type 'TestPage'

I figured out that when PageLayoutNoSidebar doesn't extend PageLayout (I copied all the code from PageLayout directly into the PageLayoutNoSidebar), this problem disappears, so I guess there is some problem with exports. 我发现,当PageLayoutNoSidebar不扩展PageLayout时(我将所有代码从PageLayout直接复制到PageLayoutNoSidebar中),此问题就消失了,所以我认为导出存在一些问题。

Do you have any idea what could be wrong with the code? 您知道代码有什么问题吗?

This is how the code of these classes looks like: 这些类的代码如下所示:

PageLayout.tsx PageLayout.tsx

import * as React from 'react';
import { Header, IHeaderActions, IHeaderProps } from '../blocks';

export interface IPageLayoutProps extends IHeaderProps {}
export interface IPageLayoutActions extends IHeaderActions {}
export interface IPageLayoutLocalState {
    headerCollapsed: boolean;
}

export
abstract class PageLayout<P extends IPageLayoutProps & IPageLayoutActions, S extends IPageLayoutLocalState>
extends React.Component<P, S>
{
    abstract render(): JSX.Element

    renderPageHeader() {
        return (
            <Header
                headerCollapsed={this.state.headerCollapsed}
                mainMenuItems={this.props.mainMenuItems}
                onHeaderToggle={this._toggleHeader}
            />
        );
    }

    renderPageContent(): JSX.Element | null {
        return null;
    }

    _toggleHeader = (headerCollapsed: boolean) => {
        this.setState({
            headerCollapsed
        });
    }

    //...
}

PageLayoutNoSidebar.tsx PageLayoutNoSidebar.tsx

import * as React from 'react';
import { IPageLayoutActions, IPageLayoutLocalState, IPageLayoutProps, PageLayout } from './PageLayout';

export
class PageLayoutNoSidebar<P extends IPageLayoutProps & IPageLayoutActions, S extends IPageLayoutLocalState>
extends PageLayout<P, S>
{
    render() {
        return (
            <div>
                {this.renderPageHeader()}
                <main className="container-narrow">
                    {this.renderPageContent()}
                </main>
            </div>
        );
    }
}

In the end i reworked the way how package is published. 最后,我重新设计了软件包的发布方式。 I set up the build process according to the https://dominicstpierre.com/using-typescript-to-publish-a-testable-react-npm-package-f3811b3c64e3 article. 我根据https://dominicstpierre.com/using-typescript-to-publish-a-testable-react-npm-package-f3811b3c64e3文章设置了构建过程。

暂无
暂无

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

相关问题 TS2339:“家”类型上不存在属性“道具” - TS2339: Property 'props' does not exist on type 'Home' 错误TS2339:类型“ Readonly &lt;{}&gt;”上不存在属性“ items” - error TS2339: Property 'items' does not exist on type 'Readonly<{}>' 错误TS2339:类型'HTMLProps <HTMLLabelElement>'上不存在属性'for' - error TS2339: Property 'for' does not exist on type 'HTMLProps<HTMLLabelElement>' TS2339(TS)属性“状态”在类型上不存在 - TS2339 (TS) Property 'state' does not exist on type 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339 TS2339:属性 &#39;defaultProps&#39; 不存在于类型 &#39;(props: any) =&gt;DetailedReactHTMLElement&lt;{ className: string; }, HTMLElement&gt;&#39; - TS2339: Property 'defaultProps' does not exist on type '(props: any) => DetailedReactHTMLElement<{ className: string; }, HTMLElement>' TS2339:类型“{}”上不存在属性X - TS2339: Property X does not exist on type '{}' TS2339:类型“ {}”上不存在属性“焦点”。 使用React打字稿 - TS2339: Property 'focus' does not exist on type '{}'. with React typescript 类型“从不”上不存在属性“单击”。 TS2339 - Property 'click' does not exist on type 'never'. TS2339 TS2339:“DefaultRootState”类型上不存在属性“数量” - TS2339: Property 'quantity' does not exist on type 'DefaultRootState'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM