import * as React from 'react'; import { OverridableComponent } from '@material-ui/core/OverridableComponent'; import { expectType } from '@material-ui/types'; interface MyOverrideProps { className: string; myString?: string; myCallback?(n: number): void; } declare const MyOverrideComponent: React.ComponentType; class MyOverrideClassComponent extends React.Component { render() { return null; } } const MyOverrideRefForwardingComponent = React.forwardRef((props, ref) => (
)); declare const MyIncompatibleComponent1: React.ComponentType<{ inconsistentProp?: number }>; declare const Foo: OverridableComponent<{ props: { numberProp: number; callbackProp?(b: boolean): void; inconsistentProp?: string; }; defaultComponent: React.ComponentType<{ defaultProp?: boolean; defaultCallbackProp?(s: string): void; }>; classKey: 'root' | 'foo' | 'bar'; }>; // Can provide basic props; callback parameter types will be inferred. console.log(b)} />; // Can pass props unique to the default component type; callback parameter types // will be inferred. console.log(s)} />; // Can override the component and pass props unique to it; props of the override // component that are provided from the wrapping component ("inner props") do // not need to be specified. ; // Can pass a callback prop with an override component; callback parameter must // be explicitly specified. console.log(n)} numberProp={3} />; // Can pass overriding component type as a parameter and callback parameters // will be inferred. component={MyOverrideComponent} myCallback={(n) => console.log(n)} numberProp={3} />; // Can provide a primitive override and an event handler with explicit type. ) => e.currentTarget.checkValidity()} />; // Can get inferred type for events by providing a component type parameter. numberProp={3} component="button" ref={(elem) => { expectType(elem); }} onClick={(e) => { expectType, typeof e>(e); e.currentTarget.checkValidity(); }} />; // Can use refs if the override is a class component numberProp={3} component={MyOverrideClassComponent} ref={(elem) => { expectType(elem); }} />; // ... or with ref-forwarding components numberProp={42} component={MyOverrideRefForwardingComponent} ref={(elem) => { expectType(elem); }} />; // ... but for an arbitrary ComponentType // @ts-expect-error component={MyOverrideComponent} ref={() => {}} />; // @ts-expect-error ; // @ts-expect-error ; console.log(n)} // n has type any numberProp={3} />; component={MyOverrideComponent} // @ts-expect-error myString={4} // should be a string myCallback={(n) => { expectType(n); }} numberProp={3} />; // @ts-expect-error ; component="div" numberProp={3} // event type doesn't match component type // @ts-expect-error onClick={(e: React.MouseEvent) => e.currentTarget.checkValidity()} />;