typescript - Material-UI 的 `withStyles` 装饰器在例子中如何打字使用?

标签 typescript material-ui

我正在尝试弄清楚如何注释以下样式的类型(我从纯 JavaScript 转换为 TypeScript 并添加了类型注释):

import * as React from 'react'
import { withStyles } from '@material-ui/core'

@withStyles(styles) // TYPE ERROR HERE
export default class App extends React.Component<{}, {}> {
    render(): JSX.Element {
        return (
            <div className="some-class"> Hello </div>
        )
    }
}

function styles() {
    return {
        // assume I need to use global styles, my actual project is more
        // complex and for some reasons I need to use global styles
        '@global': {
            '.some-class': {
                overflowX: 'hidden'
            },
        },
    }
}

我们如何输入这个?

起初,它给了我这样的错误:

        Types of property 'overflowX' are incompatible.
          Type 'string' is not assignable to type '"scroll" | "hidden" | "visible" | "auto" | "clip" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | undefined'.

所以我将其更改为以下使用 createStyles 的内容:

import * as React from 'react'
import { withStyles, createStyles } from '@material-ui/core'

@withStyles(styles) // TYPE ERROR HERE
export default class App extends React.Component<{}, {}> {
    render(): JSX.Element {
        return (
            <div className="some-class"> Hello </div>
        )
    }
}

function styles() {
    return createStyles({
        // assume I need to use global styles, my actual project is more
        // complex and for some reasons I need to use global styles
        '@global': {
            '.some-class': {
                overflowX: 'hidden'
            },
        },
    })
}

现在我得到这个错误:

tmp.tsx:5:1 - error TS1238: Unable to resolve signature of class decorator when called as an expression.
  Type 'ComponentClass<Pick<{}, never> & StyledComponentProps<"@global">, any>' is not assignable to type 'void | typeof App'.
    Type 'ComponentClass<Pick<{}, never> & StyledComponentProps<"@global">, any>' is not assignable to type 'typeof App'.
      Type 'Component<Pick<{}, never> & StyledComponentProps<"@global">, any, any>' is not assignable to type 'App'.
        Types of property 'render' are incompatible.
          Type '() => ReactNode' is not assignable to type '() => Element'.
            Type 'ReactNode' is not assignable to type 'Element'.
              Type 'undefined' is not assignable to type 'Element'.

5 @withStyles(styles) // TYPE ERROR HERE
  ~~~~~~~~~~~~~~~~~~~

在我实际的更复杂的代码中,错误是类似的,关于无法匹配 Component 类型:

App.tsx:44:1 - error TS1238: Unable to resolve signature of class decorator when called as an expression.
  Type 'ComponentClass<Pick<AppProps, never> & StyledComponentProps<"@global">, any>' is not assignable to type 'void | typeof App'.
    Type 'ComponentClass<Pick<AppProps, never> & StyledComponentProps<"@global">, any>' is not assignable to type 'typeof App'.
      Type 'Component<Pick<AppProps, never> & StyledComponentProps<"@global">, any, any>' is not assignable to type 'App'.
        Property 'makeOnStatusWindowClick' is missing in type 'Component<Pick<AppProps, never> & StyledComponentProps<"@global">, any, any>'.

44 @withStyles(styles)
   ~~~~~~~~~~~~~~~~~~~

其中 makeOnStatusWindowClick 是我的组件类中定义的第一个方法。

最佳答案

Daniel 链接到的 Material UI 文档

Unfortunately due to a current limitation of TypeScript decorators, withStyles(styles) can't be used as a decorator in TypeScript.

所以我可以通过做两件事来让它工作:

  • 在我的 styles 函数的返回对象上使用 createStyles(不要在函数上使用显式返回类型定义,以便它使用由 创建样式)
  • 使用 withStyles 作为不是装饰器
  • 使用 WithStyles 作为 Prop ,使用自动检测的 styles 类型

代码如下所示:

import * as React from 'react'
import { withStyles, WithStyles, createStyles } from '@material-ui/core'

interface Props extends WithStyles<typeof styles> {}

class App extends React.Component<Props, {}> {
    render(): JSX.Element {
        return (
            <div className="some-class"> Hello </div>
        )
    }
}

export default withStyles(styles)(App)

function styles() /*: do not put a return type here */ {
    return createStyles({
        // assume I need to use global styles, my actual project is more
        // complex and for some reasons I need to use global styles
        '@global': {
            '.some-class': {
                overflowX: 'hidden'
            },
        },
    })
}

关于typescript - Material-UI 的 `withStyles` 装饰器在例子中如何打字使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138167/

相关文章:

javascript - 为什么 TS 不生成纯 JavaScript 代码,而是生成 Node.js 函数?

javascript - 如何使用两个数组将复选框的值设置为 true?

javascript - 我想跳过对象中的最后一个属性并将其值分配给上一个属性

redux - 无法在嵌套在对话框中的连接组件中找到 "store"错误

typescript :映射类型中的索引签名

javascript - Angular 日期选择器发送太多有关日期的数据,需要转换

reactjs - 使用 defaultProps 样式化的 React Material UI v5

javascript - 找不到模块 : Can't resolve '@mui/icons-material/FileDownload'

reactjs - 更改 Material UI 的自动完成组件时如何获取特定值

css - 右侧的 Material-UI 样式按钮