TypeScript在执行以下操作时不应显示错误:
组件外部:
const useStyles = makeStyles(styles);
在组件内部:
const {/* stuff */} = useStyles();
useStyles();
函数调用带有下划线,WebStorm说“ TS2554:期望有1个参数,但有0个。” 在上面。
| 技术| 版本|
| -------------- | --------- |
| @ material-ui / styles | 3.0.0-alpha.4 |
| 反应| 16.7.0-alpha.2 |
| 打字稿| 3.1.1 |
请包括您的styles
声明和tsconfig.json
。 运行tsc
时是否出现错误? 之所以这样问,是因为IDE集成倾向于使用不同的打字稿版本。
不相关的nitpick:更喜欢解析的版本字符串react@next
随着时间的变化。 您可能是说[email protected]
吗?
我自己没有在运行tsc
,而是在每次更改TypeScript文件时都使用IDE直接将.ts/x
为.js/x
。
我正在使用的与WebStorm捆绑在一起的版本是3.1.1
。
更新了OP以反映我使用的React版本: 16.7.0-alpha.2
styles
变量是:
const styles = {
chart: {
width: '100%',
height: 70,
backgroundColor: '#f9f9f9'
},
}
(当将styles
定义为函数( theme => { /* definitions */ }
)时,我会收到相同的消息。
tsconfig.json
:
{
"compilerOptions": {
"sourceMap": false,
"target": "ES2017",
"module": "ES6",
"jsx": "react",
"moduleResolution": "Node",
"strictPropertyInitialization": true,
"strictNullChecks": true,
"noImplicitAny": true
}
}
(当将
styles
定义为函数(theme => { /* definitions */ }
)时,我得到相同的消息。
然后您的设置也有问题。 我们测试了回调用法,并且工作正常。 但是,当使用静态样式对象时,确实存在可重现的错误。
结合使用[email protected]
(complier config递增:true)和@material-ui/[email protected]
。 它确实可以与[email protected]
@TeoTN感谢您的举报。 我想我知道为什么会这样。
@TeoTN能否包含引起麻烦的代码。 我无法复制它。
@ eps1lon这是一个复制该错误的存储库: TeoTN / mui-ts-bug
我还遇到了[email protected]
和@material-ui/[email protected]
。
我从[email protected]
升级的原因是,VS Code的自动完成和工具提示速度非常慢。
降级为[email protected]
,在makeStyles
附近有新的错误,例如
Types of property 'main' are incompatible.
Type '{ position: string; top: number; left: number; bottom: number; right: number; }' is not assignable to type 'CSSProperties | ((props: {}) => CSSProperties)'.
Type '{ position: string; top: number; left: number; bottom: number; right: number; }' is not assignable to type 'CSSProperties'.
Types of property 'position' are incompatible.
Type 'string' is not assignable to type 'PositionProperty'. TS2345
9 | }));
10 |
> 11 | const useStyles = makeStyles((theme: Theme) => ({
| ^
12 | main: {
13 | position: 'absolute',
14 | top: 0,
我可以结合createStyles
来解决此问题,例如:
const useStyles = makeStyles((theme: Theme) =>
createStyles({
main: {
position: 'absolute',
top: 0,
也许值得一提的是,只有import { createStyles } from '@material-ui/styles';
有效,而createStyles
的@material-ui/core
则无效,因为类型不同。
[email protected]
和[email protected]
都不需要使用createStyles
[email protected]
。
由于此问题仅发生在打字稿的候选版本中,因此我将其关闭。 我们不能支持依赖关系的不稳定版本。 如果此错误在稳定的Typescript版本中再次出现,请提出新的问题。
打字稿3.5.1似乎是实际的,它是目前最新的稳定版本(与material-ui 4一起)
对于4.0.2版本和ts 3.5.1,我可以通过在tsconfig.json中添加“ strictNullChecks”:false来触发此错误。
对于4.0.2版本和ts 3.5.1,我可以通过在tsconfig.json中添加“ strictNullChecks”:false来触发此错误。
我们的类型不支持此功能:
我们的定义已通过以下tsconfig.json测试。 使用不太严格的tsconfig.json或省略某些库可能会导致错误。
-https://material-ui.com/guides/typescript/
types/
下发布的每个程序包也未与"strictNullChecks": false
一起测试,这意味着几乎没有程序包支持此配置。
我很惊讶。 我继承了带有strict:false的项目,以前从未产生过更多错误。
由于v3.x可以工作,因此我认为4.x也是可以的。
无论如何,该说明可能会帮助正在报告此问题的其他人,这肯定是升级的意外成本。
据我所记得,在3.x中还有其他问题。 您或者没有遇到它们,或者已经有不健全的类型。 4.x并未引入此要求。
我敢肯定有,我很幸运。
但是,如果我采用的示例项目可以用strict: true
编译,那么我不希望仅将其翻转为strict: false
就可以解决此问题。 我看不出声音类型与这种情况有什么关系。
我看不出声音类型与这种情况有什么关系。
与strictNullChecks: false
中断的实用程序类型有关。 没有该标志,可以将undefined | null
分配给any
。 这些是实施细节。
此问题需要重新打开!
我现在如何解决这个问题? 如果我传入null,makeStyles会引发“找不到未定义的类”错误。 有时我没有道具要通过,那么我该怎么做呢?
@krazyjakee尝试const c = useStyles({});
@krazyjakee尝试
const c = useStyles({});
谢谢你的工作!
我在打字稿3.7.5中遇到此错误。
这个const c = useStyles({});
有用,
但它与文档不一致:
https://material-ui.com/zh/styles/basics/
FWIW,看到同样的事情。 我已经尝试过使用strict
和strictNullChecks
张贴在这里(https://github.com/mui-org/material-ui/issues/16867)的建议,但仍然没有骰子。 现在是const c = useStyles({});
“路”了吗?
最有用的评论
@krazyjakee尝试
const c = useStyles({});