Skip to content

Commit 2fceae3

Browse files
fix(juno-core): eliminate any type inference in styled-components exports
- Fix withTooltip HOC to properly infer component types using ElementRef and ComponentPropsWithoutRef - Fix styled-components.ts to export typed styled/css/keyframes instead of ThemedStyledComponentsModule - Add @types/styled-components to devDependencies (was only in resolutions) - Fix SubMenu TransitionProps optional typing This ensures RcIconButton and other components export correct StyledComponent types instead of any in the generated .d.ts files.
1 parent 80cc098 commit 2fceae3

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"type-fest": "2.17.0"
5555
},
5656
"devDependencies": {
57+
"@types/styled-components": "5.1.36",
5758
"@nrwl/cli": "^14.5.1",
5859
"@nrwl/workspace": "^14.5.1",
5960
"@percy/cli": "1.7.0",

packages/juno-core/src/components/Menu/SubMenu/SubMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ const _RcSubMenu = forwardRef<any, RcSubMenuProps>(
358358
onKeyDown={handlePopperKeyDown}
359359
onMouseLeave={handlePopperMouseLeave}
360360
>
361-
{({ TransitionProps }: { TransitionProps: object }) => (
361+
{({ TransitionProps }: { TransitionProps?: object }) => (
362362
<StyledGrow {...TransitionProps} timeout="auto">
363363
<RcPaper>
364364
<RcSubMenuContext.Provider value={ctxValue}>

packages/juno-core/src/components/Tooltip/withTooltip/withTooltip.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import React, { ComponentType, forwardRef } from 'react';
1+
import React, { forwardRef } from 'react';
2+
import type {
3+
ComponentPropsWithoutRef,
4+
ElementRef,
5+
ComponentType,
6+
} from 'react';
27

38
import { RcTooltip, RcTooltipProps } from '../Tooltip';
49

@@ -14,20 +19,36 @@ type WithTooltipProps<P extends {} = {}> = {
1419
/**
1520
* Make a component can be use with `RcTooltip` and Tooltip Props
1621
*/
17-
function withTooltip<T extends object>(Component: ComponentType<T>) {
18-
return forwardRef<any, WithTooltipProps<T>>((props, ref) => {
22+
function withTooltip<C extends ComponentType<unknown>>(Component: C) {
23+
type ComponentProps = ComponentPropsWithoutRef<C>;
24+
type SafeProps = ComponentProps extends object ? ComponentProps : {};
25+
type Props = WithTooltipProps<SafeProps>;
26+
type Ref = ElementRef<C>;
27+
28+
const WithTooltip = forwardRef<Ref, Props>((props, ref) => {
1929
const { title, useRcTooltip, TooltipProps, ...rest } = props;
30+
const restProps = rest as SafeProps;
31+
const ComponentWithRef = Component as unknown as React.ComponentType<
32+
SafeProps & { ref?: React.Ref<Ref> }
33+
>;
2034

2135
if (title && useRcTooltip) {
2236
return (
2337
<RcTooltip title={title as RcTooltipProps['title']} {...TooltipProps}>
24-
<Component {...(rest as any)} ref={ref} />
38+
<ComponentWithRef {...restProps} ref={ref} />
2539
</RcTooltip>
2640
);
2741
}
2842

29-
return <Component title={title} {...(rest as any)} ref={ref} />;
43+
return (
44+
<ComponentWithRef {...({ ...restProps, title } as SafeProps)} ref={ref} />
45+
);
3046
});
47+
48+
return WithTooltip as React.ForwardRefExoticComponent<
49+
React.PropsWithoutRef<Props> & React.RefAttributes<Ref>
50+
> &
51+
React.ComponentType<Props>;
3152
}
3253

3354
export { withTooltip };

packages/juno-core/src/foundation/styled-components.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
// @ts-ignore - styled-components 5.x bundled types
1+
import type { ComponentType, ReactNode } from 'react';
2+
23
import type {
34
FlattenInterpolation,
45
Interpolation,
56
InterpolationValue,
67
Keyframes,
8+
ThemedCssFunction,
79
ThemedStyledComponentsModule,
10+
ThemedStyledInterface,
811
ThemedStyledProps,
912
ThemeProps as StyledThemeProps,
10-
// @ts-ignore
1113
} from 'styled-components';
1214
/* eslint-disable import/no-duplicates */
13-
// @ts-ignore - styled-components 5.x type import workaround
1415
import * as styledComponents from 'styled-components';
1516

1617
import { useTheme } from '@material-ui/core/styles';
@@ -19,20 +20,26 @@ import { RcTheme } from './theme/theme.type';
1920

2021
type ThemeProps = StyledThemeProps<RcTheme>;
2122

22-
const {
23-
default: styled,
24-
css,
25-
keyframes,
26-
createGlobalStyle,
27-
withTheme,
28-
// @ts-ignore
29-
StyleSheetManager,
30-
ThemeProvider,
31-
ThemeConsumer,
32-
} = styledComponents as any as ThemedStyledComponentsModule<RcTheme>;
23+
const styledModule =
24+
styledComponents as unknown as ThemedStyledComponentsModule<RcTheme>;
25+
const styled: ThemedStyledInterface<RcTheme> = styledModule.default;
26+
const css: ThemedCssFunction<RcTheme> = styledModule.css;
27+
const keyframes: typeof styledModule.keyframes = styledModule.keyframes;
28+
const createGlobalStyle: typeof styledModule.createGlobalStyle =
29+
styledModule.createGlobalStyle;
30+
const withTheme: typeof styledModule.withTheme = styledModule.withTheme;
31+
const StyleSheetManager = (
32+
styledComponents as typeof styledComponents & {
33+
StyleSheetManager: ComponentType<{ children?: ReactNode }>;
34+
}
35+
).StyleSheetManager;
36+
const ThemeProvider: typeof styledModule.ThemeProvider =
37+
styledModule.ThemeProvider;
38+
const ThemeConsumer: typeof styledModule.ThemeConsumer =
39+
styledModule.ThemeConsumer;
3340

3441
type Dependencies = {
35-
dependencies?: (React.ComponentType | ((props: any) => JSX.Element))[];
42+
dependencies?: (ComponentType | ((props: unknown) => JSX.Element))[];
3643
};
3744

3845
const RcUseTheme = <T = RcTheme>() => useTheme<T>();

sync-github.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"latestCommitSHA": "027f472e1541e6a9c04a57e3dd7cba06d03ee623"
2+
"latestCommitSHA": "11323eba75a80e15f876eb4a27bbfbe7830a7048"
33
}

0 commit comments

Comments
 (0)