gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:20 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何注释以下样式的类型(我从纯 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com