如何解决”argument of type ‘boolean’ is not assignable…”错误。

huangapple go评论143阅读模式
英文:

How to resolve "argument of type 'boolean' is not assignable..."

问题

以下是您要翻译的内容:

"I've an input component and I'd like to create unit tests for it. I'm having few errors and I can't fix it. I already see in few blogs, but I don't understand how to resolve it. I'm trying to improve my knowledge in unit tests, but it's complicated.

Here's my code I put into codesandbox:

如何解决”argument of type ‘boolean’ is not assignable…”错误。

I don't understand why I've this error, because I don't have any Boolean parameter. How can I resolve it?


import React from 'react';
import { render, screen } from '@testing-library/react';
import TextInput from './TextInput';
import { TextInputProps } from 'src/types';

describe('Text Input', function () {
it('should render default', function () {
const props: TextInputProps = {
change(): void {},
value: '',
placeholder: 'Full Name',
variant: 'neutral',
};

render(<TextInput {...props} />);
expect(screen.getByRole('textbox')).toBeInTheDocument();
expect(screen.getByRole('textbox')).toHaveAttribute(
  'placeholder',
  'Full Name'
);
expect(screen.getByRole('textbox')).toHaveAttribute('value', '');

});
});



import React from 'react';
import { TextInputProps } from 'src/types';
import './index.scss';

const TextInput = ({
value,
placeholder,
type = 'text',
variant,
change,
}: TextInputProps) => {
const inputVariant = input-field ${ variant === 'highlight' ? 'highlight' : '' };

return (

);
};

export default TextInput;



import { ChangeEventHandler } from 'react';

export interface TextInputProps {
value: string;
placeholder: string;
type?: 'text' | 'password' | 'email';
variant: 'neutral' | 'highlight';
change: ChangeEventHandler;
}

如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

I've an input component and I'd like to create unit tests for it. I'm having few errors and I can't fix it. I already see in few blogs, but I don't understand how to resolve it. I'm trying to improve my knowledge in unit tests, but it's complicated.

Here's my code I put into codesandbox:

如何解决”argument of type ‘boolean’ is not assignable…”错误。

I don't understand why I've this error, because I don't have any Boolean parameter. How can I resolve it?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React from &#39;react&#39;;
import { render, screen } from &#39;@testing-library/react&#39;;
import TextInput from &#39;./TextInput&#39;;
import { TextInputProps } from &#39;src/types&#39;;

describe(&#39;Text Input&#39;, function () {
  it(&#39;should render default&#39;, function () {
    const props: TextInputProps = {
      change(): void {},
      value: &#39;&#39;,
      placeholder: &#39;Full Name&#39;,
      variant: &#39;neutral&#39;,
    };

    render(&lt;TextInput {...props} /&gt;);
    expect(screen.getByRole(&#39;textbox&#39;)).toBeInTheDocument();
    expect(screen.getByRole(&#39;textbox&#39;)).toHaveAttribute(
      &#39;placeholder&#39;,
      &#39;Full Name&#39;
    );
    expect(screen.getByRole(&#39;textbox&#39;)).toHaveAttribute(&#39;value&#39;, &#39;&#39;);
  });
});

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React from &#39;react&#39;;
import { TextInputProps } from &#39;src/types&#39;;
import &#39;./index.scss&#39;;

const TextInput = ({
  value,
  placeholder,
  type = &#39;text&#39;,
  variant,
  change,
}: TextInputProps) =&gt; {
  const inputVariant = `input-field ${
    variant === &#39;highlight&#39; ? &#39;highlight&#39; : &#39;&#39;
  }`;

  return (
    &lt;div className=&quot;content-input&quot;&gt;
      &lt;input
        className={inputVariant}
        value={value}
        placeholder={placeholder}
        type={type}
        onChange={change}
      /&gt;
    &lt;/div&gt;
  );
};

export default TextInput;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { ChangeEventHandler } from &#39;react&#39;;

export interface TextInputProps {
  value: string;
  placeholder: string;
  type?: &#39;text&#39; | &#39;password&#39; | &#39;email&#39;;
  variant: &#39;neutral&#39; | &#39;highlight&#39;;
  change: ChangeEventHandler&lt;HTMLInputElement&gt;;
}

<!-- end snippet -->

答案1

得分: 2

使用 .tsx 文件,而不是 .ts 文件。

在您发送的示例中,我能够重现错误,然后注意到您正在使用 .ts 文件。将其重命名为 .tsx,错误就会消失。

英文:

Use a .tsx file, not a .ts one.

On the example you sent, I was able to reproduce the error, then noticed you were using a .ts file. Rename it to .tsx and the error is gone.

huangapple
  • 本文由 发表于 2023年5月29日 23:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定