-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript: in ListItem, property button is no more boolean #14971
Comments
I fail to reproduce the problem. Do you have a reproduction? |
Thank you @oliviertassinari for looking at it. I cannot reproduce on CodeSandbox, even if I use the same configuration as home. 😥 I made a minimal project where the compilation fails because of this issue (see its README in order to see how to use it): |
@sveyret Please add a lockfile to your repository. Otherwise changed dependencies over time might change to result. |
@eps1lon Done. |
Thank you @eps1lon, I hadn't notice it became a discriminent. I'll update my code for that. |
We have added a test which includes a workaround (with runtime overhead) for this issue. If anybody can come up with a solution that breaks the test in #15049 but passes all other tests we would be happy to accept PRs with that patch. |
I have the same issue with |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
In case it got drowned out: The latest status is #14971 (comment). |
Probably a silly question - declare const ListItem: OverridableComponent<ListItemTypeMap<{ button?: false }, 'li'>> &
ExtendButtonBase<ListItemTypeMap<{ button: true }, 'div'>>; I wonder why we are explicitly putting |
See http://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions |
This comment has been minimized.
This comment has been minimized.
In my case, this particular issue causes errors with ListItem's "component" prop as well. If I manually edit the |
Having the same issue, I'm trying to do
and I get the error message Seems pretty counter-intuitive to me |
Getting a similar problem when using
which gives me the error |
@ErwanDL For me a cast worked:
|
Can't the evolution provided by TS 3.5 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#smarter-union-type-checking provide a way to correct this? |
Since we rely on function overloads the feature in 3.5 does not help us. It only seems to apply to object property checking. |
The following "solution" doesn't fix the root problem with the TS MUI types but provides a way to temporarily overcome the typescript errors. Declare a custom interface: Create a new component wrapping the original material-ui component and pass in the TS interface: |
Landed here after trying to calculate if a I'd like to be able to do something like [Edit] Of course as soon as I typed this up I thought, "why not just make your bool 'any'?" That works. So now I'm using this to toggle the button: |
As mentioned above it's just a downside of using boolean literals as a discriminator. It's necessary to conditionally show what props are available. Either be explicit:
Or cast to any:
|
@eps1lon thank you for pointing me to the same issue. IMHO, casting with
Typescript sometimes makes me scratch my head as this issue does to me. I was thinking about using enum ClassOfBook {
YES = 'yes',
NO = 'no'
}
interface Book {
isBook: ClassOfBook
}
interface OverloadedComponent {
(props: Book): void;
}
declare const SomeComponent: OverloadedComponent;
function Library(props: Book) {
const { isBook } = props;
SomeComponent(props);
SomeComponent({ isBook: ClassOfBook.YES });
} |
Furthermore, ts can't compile styled ListItem with button prop that is set to inline false |
If you are looking for workaround, something like this works:
|
I think this issue can be closed once #26446 merged. Summary
// before
<ListItem button>
// v5
<ListItemButton> |
The following code in not compiling anymore in mui v4.0:
where
editable
is a boolean value.Expected Behavior 🤔
Should compile without error.
Current Behavior 😯
Type 'boolean' is not assignable to type 'true'.
button={editable}
../../../node_modules/@material-ui/core/ListItem/ListItem.d.ts:23:38
23 ExtendButtonBase<ListItemTypeMap<{ button: true }, 'div'>>;
The expected type comes from property 'button' which is declared here on type 'IntrinsicAttributes & { action?: ((actions: ButtonBaseActions) => void) | undefined; buttonRef?: ((instance: any) => void) | RefObject | null | undefined; centerRipple?: boolean | undefined; ... 6 more ...; TouchRippleProps?: Partial<...> | undefined; } & { ...; } & { ...; } & CommonProps<...> & Pick<...>'
Your Environment 🌎
The text was updated successfully, but these errors were encountered: