question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TextField TS Error with `variant` prop

See original GitHub issue

When passing a conditional statement to the variant prop of the TextField component like this:

<TextField
  variant={isLongTextQuestion ? "outlined" : "standard"}
  ...
/>

I get a typescript error saying:

Types of property 'variant' are incompatible.
      Type '"standard" | "outlined"' is not assignable to type '"outlined"'.
        Type '"standard"' is not assignable to type '"outlined"'.ts(2322)
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

TextField component should allow this statement as it accepts both outlined and standard independently.

Current Behavior 😯

Gives typescript type error

Steps to Reproduce 🕹

Link:

https://codesandbox.io/s/mor0mrooz9

Context 🔦

Your Environment 🌎

Tech Version
Material-UI v3.9.3
React v16.8.4
Browser
TypeScript
etc.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:16
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
TidyIQcommented, Nov 15, 2019

I figured out a workaround. For anyone else who sees this, try this:

interface MyTextInputProps extends BaseTextFieldProps {
  readonly foo: number;
}

const MyTextInput: FC<MyTextInputProps> = ({ foo, ...props }) => {
  const newLabel = foo.toString();

  /**
   * Hack to make 'props.variant' type safe
   *
   * See: https://github.com/mui-org/material-ui/issues/15697
   */
  const tsProps = (() => {
    let tsVariant;
    switch (variant) {
      case "outlined": {
        tsVariant = { variant: variant as "outlined" };
        break;
      }
      case "filled": {
        tsVariant = { variant: "filled" as "filled" };
        break;
      }
      case "standard":
      default: {
        tsVariant = { variant: "standard" as "standard" };
        break;
      }
    }
    const p = props;
    delete p.variant;
    return { ...p, ...tsVariant };
  })();
  return <TextInput label={newLabel} {...tsProps} />;
}

It’s far from ideal though as the only purpose of this entire function is to fix the props.variant bug.

7reactions
ivorobioffcommented, Apr 11, 2020

fuck dat and do this:

<TextField variant={control.textFieldVariant as any} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I pass in the variant property of the material-ui ...
OutlinesTextFieldProps needs a value of 'outlined' for variant ... The issue is that the value of variant is not known at compile time....
Read more >
TextField API - Material UI - MUI
Name Type Default autoComplete string autoFocus bool false classes object
Read more >
Useful Patterns by Use Case - React TypeScript Cheatsheets
{/* Error: Property 'toPrecision' does not exist on type 'string'. ... As of TS 2.9, you can also supply the type parameter in...
Read more >
Input - Chakra UI
The input component comes in 4 variants: outline , unstyled , flushed , and filled . Pass the variant prop and set it...
Read more >
API - React Select
These base props are those available to be passed to all select variants. ... HTML ID of an element containing an error message...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found