【发布时间】:2018-11-04 05:57:33
【问题描述】:
我正在尝试弄清楚如何注释以下样式的类型(我从纯 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 是我的组件类中定义的第一个方法。
【问题讨论】: