Skip to content
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

[styled-engine-sc] Fix theme being of type any #40200

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 69 additions & 16 deletions packages/mui-styled-engine-sc/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ type ThemedStyledComponentFactories<T extends object> = {
export type StyledComponentPropsWithRef<
C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
> = C extends AnyStyledComponent
? React.ComponentPropsWithRef<StyledComponentInnerComponent<C>>
: React.ComponentPropsWithRef<C>;
? React.ComponentPropsWithRef<StyledComponentInnerComponent<C>>
: React.ComponentPropsWithRef<C>;
mnajdova marked this conversation as resolved.
Show resolved Hide resolved

// Same as in styled-components, but copied here so that it would use the Interpolation & CSS typings from above
export interface ThemedStyledFunctionBase<
Expand Down Expand Up @@ -292,32 +292,85 @@ export interface StyledConfig<O extends object = {}> {
// TODO: Add all types from the original StyledComponentWrapperProperties
componentId?: string;
displayName?: string;
label?: string;
target?: string;
shouldForwardProp?:
| ((prop: keyof O, defaultValidatorFn: (prop: keyof O) => boolean) => boolean)
| undefined;
}

/** Same as StyledConfig but shouldForwardProp must be a type guard */
export interface FilteringStyledOptions<Props, ForwardedProps extends keyof Props = keyof Props> {
componentId?: string;
displayName?: string;
label?: string;
shouldForwardProp?(propName: PropertyKey): propName is ForwardedProps;
target?: string;
}

// same as ThemedBaseStyledInterface in styled-components, but with added options & common props for MUI components
export interface ThemedBaseStyledInterface<
MUIStyledCommonProps extends object,
MuiStyledOptions extends object,
T extends object,
> extends ThemedStyledComponentFactories<T> {
<C extends AnyStyledComponent>(
Theme extends object,
> extends ThemedStyledComponentFactories<Theme> {
<
C extends React.ComponentClass<React.ComponentProps<C>>,
ForwardedProps extends keyof React.ComponentProps<C> = keyof React.ComponentProps<C>,
>(
component: C,
options?: StyledConfig<any> & MuiStyledOptions,
): ThemedStyledFunction<
StyledComponentInnerComponent<C>,
T,
StyledComponentInnerOtherProps<C> & MUIStyledCommonProps,
StyledComponentInnerAttrs<C>
options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps> & MuiStyledOptions,
): CreateStyledComponent<
Pick<PropsOf<C>, ForwardedProps> & MUIStyledCommonProps,
{},
{
ref?: React.Ref<InstanceType<C>>;
},
Theme
>;
<C extends keyof JSX.IntrinsicElements | React.ComponentType<any>>(
// unfortunately using a conditional type to validate that it can receive a `theme?: Theme`
// causes tests to fail in TS 3.1

<C extends React.ComponentClass<React.ComponentProps<C>>>(
component: C,
options?: StyledConfig<any> & MuiStyledOptions,
): ThemedStyledFunction<C, T, MUIStyledCommonProps>;
options?: StyledConfig<PropsOf<C> & MUIStyledCommonProps> & MuiStyledOptions,
): CreateStyledComponent<
PropsOf<C> & MUIStyledCommonProps,
{},
{
ref?: React.Ref<InstanceType<C>>;
},
Theme
>;

<
C extends React.JSXElementConstructor<React.ComponentProps<C>>,
ForwardedProps extends keyof React.ComponentProps<C> = keyof React.ComponentProps<C>,
>(
component: C,
options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps> & MuiStyledOptions,
): CreateStyledComponent<Pick<PropsOf<C>, ForwardedProps> & MUIStyledCommonProps, {}, {}, Theme>;

<C extends React.JSXElementConstructor<React.ComponentProps<C>>>(
component: C,
options?: StyledConfig<PropsOf<C> & MUIStyledCommonProps> & MuiStyledOptions,
): CreateStyledComponent<PropsOf<C> & MUIStyledCommonProps, {}, {}, Theme>;

<
Tag extends keyof JSX.IntrinsicElements,
ForwardedProps extends keyof JSX.IntrinsicElements[Tag] = keyof JSX.IntrinsicElements[Tag],
>(
tag: Tag,
options: FilteringStyledOptions<JSX.IntrinsicElements[Tag], ForwardedProps> & MuiStyledOptions,
): CreateStyledComponent<
MUIStyledCommonProps,
Pick<JSX.IntrinsicElements[Tag], ForwardedProps>,
{},
Theme
>;

<Tag extends keyof JSX.IntrinsicElements>(
tag: Tag,
options?: StyledConfig<MUIStyledCommonProps> & MuiStyledOptions,
): CreateStyledComponent<MUIStyledCommonProps, JSX.IntrinsicElements[Tag], {}, Theme>;
}

export type CreateMUIStyled<
Expand Down