Material-ui: “ TS2554:期望有1个参数,但有0个。” 在makeStyles返回的钩子上

创建于 2018-12-28  ·  22评论  ·  资料来源: mui-org/material-ui

  • [x]这不是v0.x问题。
  • [x]我搜索了此存储库的问题,并认为这不是重复的。

预期行为🤔

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 |

bug 🐛 styles typescript

最有用的评论

@krazyjakee尝试const c = useStyles({});

所有22条评论

请包括您的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,看到同样的事情。 我已经尝试过使用strictstrictNullChecks张贴在这里(https://github.com/mui-org/material-ui/issues/16867)的建议,但仍然没有骰子。 现在是const c = useStyles({}); “路”了吗?

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

mattmiddlesworth picture mattmiddlesworth  ·  3评论

TimoRuetten picture TimoRuetten  ·  3评论

revskill10 picture revskill10  ·  3评论

ghost picture ghost  ·  3评论

sys13 picture sys13  ·  3评论