Skip to content

Commit

Permalink
[react@18] Implicit children type fixes (elastic#192011)
Browse files Browse the repository at this point in the history
## Summary

Part of elastic#138222

in @types/react@18 types [have become more
strict](DefinitelyTyped/DefinitelyTyped#56210).
This PR addresses a bunch of easy fixes. The most common are:

### 1 Removal of implicit children

For components that do implement children but relied on their implicit
declaration from React.FunctionComponent or React.Component:

```diff
 interface Props {
+  children?: React.ReactNode;
 }

 class SomeClassComponents React.Component<Props> {
   render() {
     return  <div>{this.props.children}</div>
   }
 }
 const SomeFunctionComponent: React.FunctionComponent<Props> = props => <div>{props.children}</div>
```

or 

```diff
- const SomeFunctionComponent: React.FunctionComponent<Props> = props => <div>{props.children}</div>
+ const SomeFunctionComponent: React.FunctionComponent<React.PropsWithChildren<Props>> = props => <div>{props.children}</div>
```


*Note 1:*
The most common change occurs in unit tests where `renderHook` and
`wrapper` are used. I had to re-type the props so that `children` were
there

```diff
const { result } = renderHook(
         () => {
           return useLicense();
         },
         {
-           wrapper: ({ children }) => (
+           wrapper: ({ children }: React.PropsWithChildren<{}>) => (
             <TestProviders license={license}>{children}</TestProviders>
           ),
         }
       );
```

```diff
- const { result } = renderHook<GetCasesColumn, UseCasesColumnsReturnValue>(
+ const { result } = renderHook<React.PropsWithChildren<GetCasesColumn>, UseCasesColumnsReturnValue>(
       () => useCasesColumns(defaultColumnArgs),
       {
         wrapper: ({ children }) => <TestProviders>{children}</TestProviders>,
       }
     );
```



*Note 2:*
With @types/react@17 the components that don't have any props apart from
`children` I had to use `React.PropsWithChildren<{}>`, whereas in
@types/react@18 the argument becomes optional, so it can be omitted, and
type simpler with `React.PropsWithChildren` without an argument



### 2 `this.context` becomes `unknown` (was `any`)

In a couple of places where `this.context` is used, I had to type it

### 3 `ComponentType` from enzyme is no longer compatible with
`ComponentType` from react

This one is a bummer, but where `wrappingComponent` with enzyme is used
I had to cast it to type from `enzyme`

```diff
- import { mount } from 'enzyme'
+ import { mount, ComponentType } from 'enzyme'

 wrapper = mount(<ClosureOptions {...props} />, {
-       wrappingComponent: TestProviders,
+       wrappingComponent: TestProviders as ComponentType<{}>,
});
```
  • Loading branch information
Dosant authored Sep 9, 2024
1 parent a602eed commit fccfd4c
Show file tree
Hide file tree
Showing 197 changed files with 1,153 additions and 712 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface SpaNoRouterLinkProps {
'data-test-subj'?: string;
}

export const SpaNoRouterLink: React.FC<SpaNoRouterLinkProps> = ({
export const SpaNoRouterLink: React.FC<React.PropsWithChildren<SpaNoRouterLinkProps>> = ({
url,
go,
onClick,
Expand Down
7 changes: 3 additions & 4 deletions packages/cloud/connection_details/context/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import { context as serviceContext } from './service';

export const context = React.createContext<ConnectionDetailsOpts>({});

export const ConnectionDetailsOptsProvider: React.FC<ConnectionDetailsOpts> = ({
children,
...opts
}) => {
export const ConnectionDetailsOptsProvider: React.FC<
React.PropsWithChildren<ConnectionDetailsOpts>
> = ({ children, ...opts }) => {
const service = React.useMemo(() => new ConnectionDetailsService(opts), [opts]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ export interface KibanaConnectionDetailsProviderProps {
};
}

export const KibanaConnectionDetailsProvider: React.FC<KibanaConnectionDetailsProviderProps> = (
props
) => {
export const KibanaConnectionDetailsProvider: React.FC<
React.PropsWithChildren<KibanaConnectionDetailsProviderProps>
> = (props) => {
const opts = useAsyncMemo(
() => createOpts(props),
[props.onNavigation, props.options, props.start]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type KibanaWiredConnectionDetailsProviderProps = Omit<
>;

export const KibanaWiredConnectionDetailsProvider: React.FC<
KibanaWiredConnectionDetailsProviderProps
React.PropsWithChildren<KibanaWiredConnectionDetailsProviderProps>
> = (props) => {
return (
<KibanaConnectionDetailsProvider {...props} start={getGlobalDependencies().start}>
Expand Down
10 changes: 7 additions & 3 deletions packages/cloud/connection_details/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const defaultOpts: ConnectionDetailsOpts = {
},
};

export const StoriesProvider: React.FC<Partial<ConnectionDetailsOpts>> = ({
export const StoriesProvider: React.FC<React.PropsWithChildren<Partial<ConnectionDetailsOpts>>> = ({
children,
...rest
}) => {
Expand All @@ -53,7 +53,9 @@ export const StoriesProvider: React.FC<Partial<ConnectionDetailsOpts>> = ({
);
};

export const StoriesProviderKeyCreationError: React.FC = ({ children }) => {
export const StoriesProviderKeyCreationError: React.FC<React.PropsWithChildren<{}>> = ({
children,
}) => {
const opts: ConnectionDetailsOpts = {
...defaultOpts,
apiKeys: {
Expand All @@ -68,7 +70,9 @@ export const StoriesProviderKeyCreationError: React.FC = ({ children }) => {
return <ConnectionDetailsOptsProvider {...opts}>{children}</ConnectionDetailsOptsProvider>;
};

export const StoriesProviderNoKeyPermissions: React.FC = ({ children }) => {
export const StoriesProviderNoKeyPermissions: React.FC<React.PropsWithChildren<{}>> = ({
children,
}) => {
const opts: ConnectionDetailsOpts = {
...defaultOpts,
apiKeys: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface Props {

const isFormFieldValid = (field: Field) => !Boolean(field.errors?.length);

export const MetadataForm: FC<Props> = ({
export const MetadataForm: FC<React.PropsWithChildren<Props>> = ({
form,
tagsReferences,
TagList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface FavoritesContextValue {

const FavoritesContext = React.createContext<FavoritesContextValue | null>(null);

export const FavoritesContextProvider: React.FC<FavoritesContextValue> = ({
export const FavoritesContextProvider: React.FC<React.PropsWithChildren<FavoritesContextValue>> = ({
favoritesClient,
notifyError,
children,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ interface Context {

const TagFilterContext = React.createContext<Context | null>(null);

export const TagFilterContextProvider: FC<Context> = ({ children, ...props }) => {
export const TagFilterContextProvider: FC<React.PropsWithChildren<Context>> = ({
children,
...props
}) => {
return <TagFilterContext.Provider value={props}>{children}</TagFilterContext.Provider>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ interface Context {

const UserFilterContext = React.createContext<Context | null>(null);

export const UserFilterContextProvider: FC<Context> = ({ children, ...props }) => {
export const UserFilterContextProvider: FC<React.PropsWithChildren<Context>> = ({
children,
...props
}) => {
if (!props.enabled) {
return <>{children}</>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function verifyTextAndTitle(
expect(text).toEqual(expectedText);
}

function getNodeText(node: ReactNode) {
function getNodeText(node: ReactNode | MountPoint) {
const rendered = render(node as ReactElement);
return rendered.text();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import React, { FunctionComponent } from 'react';
import React from 'react';
import { AlertConsumers } from '@kbn/rule-data-utils';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks/dom';
Expand Down Expand Up @@ -46,7 +46,7 @@ mockServices.dataViewsService.create.mockResolvedValue(mockDataView);

const queryClient = new QueryClient(testQueryClientConfig);

const wrapper: FunctionComponent = ({ children }) => (
const wrapper: React.FC<React.PropsWithChildren<{}>> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const ruleToCreate: CreateRuleBody<RuleTypeParams> = {

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import React, { FunctionComponent } from 'react';
import React, { FC } from 'react';
import { AlertConsumers } from '@kbn/rule-data-utils';
import * as ReactQuery from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
Expand All @@ -19,7 +19,7 @@ const { QueryClient, QueryClientProvider } = ReactQuery;

const queryClient = new QueryClient(testQueryClientConfig);

const wrapper: FunctionComponent = ({ children }) => (
const wrapper: FC<React.PropsWithChildren<{}>> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down Expand Up @@ -62,7 +62,10 @@ describe('useFetchAlertsFieldsQuery', () => {

it('should correctly override the `enabled` option', () => {
const { rerender } = renderHook(
({ featureIds, enabled }: { featureIds: AlertConsumers[]; enabled?: boolean }) =>
({
featureIds,
enabled,
}: React.PropsWithChildren<{ featureIds: AlertConsumers[]; enabled?: boolean }>) =>
useFetchAlertsFieldsQuery({ http: mockHttpClient, featureIds }, { enabled }),
{
wrapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock('../apis/fetch_alerts_index_names');

const queryClient = new QueryClient(testQueryClientConfig);

const wrapper: FunctionComponent = ({ children }) => (
const wrapper: FunctionComponent<React.PropsWithChildren<{}>> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const queryClient = new QueryClient({
},
});

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const { fetchAlertingFrameworkHealth } = jest.requireMock(

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useLoadActionTypes } from './use_load_connector_types';

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useLoadConnectors } from './use_load_connectors';

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useLoadRuleTypeAadTemplateField } from './use_load_rule_type_aad_templa

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useResolveRule } from './use_resolve_rule';

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('useSearchAlertsQuery', () => {
sort: [],
};

const wrapper: FunctionComponent = ({ children }) => (
const wrapper: FunctionComponent<React.PropsWithChildren<{}>> = ({ children }) => (
<QueryClientProvider client={queryClient} context={AlertsQueryContext}>
{children}
</QueryClientProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const ruleToUpdate: UpdateRuleBody<RuleTypeParams> = {

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const useQuerySpy = jest.spyOn(ReactQuery, 'useQuery');

const queryClient = new QueryClient(testQueryClientConfig);

const wrapper: FunctionComponent = ({ children }) => (
const wrapper: FunctionComponent<React.PropsWithChildren<{}>> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand All @@ -38,7 +38,7 @@ describe('useVirtualDataViewQuery', () => {

it('does not create a data view if indexNames is empty or nullish', () => {
const { rerender } = renderHook(
({ indexNames }: { indexNames: string[] }) =>
({ indexNames }: React.PropsWithChildren<{ indexNames: string[] }>) =>
useVirtualDataViewQuery({ dataViewsService: mockDataViewsService, indexNames }),
{
wrapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ getAvailableRuleTypes.mockReturnValue([

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: Node }) => (
const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
CIRCUIT_BREAKER_SEE_FULL_ERROR_TEXT,
} from '../translations';

export const RuleFormCircuitBreakerError: FC<{}> = ({ children }) => {
export const RuleFormCircuitBreakerError: FC<React.PropsWithChildren<{}>> = ({ children }) => {
const [showDetails, setShowDetails] = useState(false);

const onToggleShowDetails = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ interface RuleFormErrorPromptWrapperProps {
hasShadow?: boolean;
}

export const RuleFormErrorPromptWrapper: React.FC<RuleFormErrorPromptWrapperProps> = ({
children,
hasBorder,
hasShadow,
}) => {
export const RuleFormErrorPromptWrapper: React.FC<
React.PropsWithChildren<RuleFormErrorPromptWrapperProps>
> = ({ children, hasBorder, hasShadow }) => {
const styles = useEuiBackgroundColorCSS().transparent;
return (
<EuiPageTemplate offset={0} css={styles}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export interface RuleFormStateProviderProps {
initialRuleFormState: RuleFormState;
}

export const RuleFormStateProvider: React.FC<RuleFormStateProviderProps> = (props) => {
export const RuleFormStateProvider: React.FC<
React.PropsWithChildren<RuleFormStateProviderProps>
> = (props) => {
const { children, initialRuleFormState } = props;
const {
formData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const tooltipContent: CaseTooltipContentProps = {
};

const tooltipProps: CaseTooltipProps = {
children: TestSpan,
children: <TestSpan />,
loading: false,
content: tooltipContent,
};

const longTitle = `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
const longTitle = `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry standard dummy text ever since the 1500s!! Lorem!!!`;

const longDescription = `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer
took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,
const longDescription = `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer
took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,
but also the leap into electronic typesetting, remaining essentially unchanged.`;

const Template = (args: CaseTooltipProps) => (
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-cases-components/src/tooltip/tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const tooltipContent: CaseTooltipContentProps = {
};

const tooltipProps: CaseTooltipProps = {
children: TestSpan,
children: <TestSpan />,
loading: false,
content: tooltipContent,
};
Expand Down
Loading

0 comments on commit fccfd4c

Please sign in to comment.