Skip to content

Commit 222e6fb

Browse files
Fix TypeScript compiler issues
1 parent 5c6e5b2 commit 222e6fb

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

.storybook/manager.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { addons } from '@storybook/manager-api';
22
import nhsTheme from './theme';
33
import { startCase, upperFirst } from 'lodash';
44

5-
const sentenceCase = (string) => {
6-
if (typeof string !== 'string') return '';
7-
return upperFirst(startCase(string).toLowerCase());
5+
const sentenceCase = (name = '') => {
6+
if (!name || typeof name !== 'string') {
7+
return '';
8+
}
9+
10+
return upperFirst(startCase(name).toLowerCase());
811
};
912

1013
addons.setConfig({

src/components/content-presentation/table/TableHelpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Children, isValidElement, type ReactElement, type ReactNode } from 'react';
1+
import { Children, type ReactElement, type ReactNode } from 'react';
22
import { TableCell, type TableCellProps } from './components/TableCell';
3+
import { childIsOfComponentType } from '#util/types/TypeGuards';
34

45
export const isTableCell = (
56
child: ReactNode,
67
): child is ReactElement<TableCellProps, typeof TableCell> => {
7-
return isValidElement(child) && child.type === TableCell;
8+
return childIsOfComponentType(child, TableCell);
89
};
910

1011
export const getHeadingsFromChildren = (children?: ReactNode) => {

src/components/navigation/card/components/CardGroupItem.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import classNames from 'classnames';
2-
import { Col } from '#components/layout';
2+
import { type FC } from 'react';
3+
import { Col, type ColProps } from '#components/layout';
34

4-
export const CardGroupItem: typeof Col = ({ className, ...rest }) => (
5+
export const CardGroupItem: FC<ColProps> = ({ className, ...rest }) => (
56
<Col className={classNames('nhsuk-card-group__item', className)} {...rest} />
67
);
78

src/util/types/TypeGuards.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
import {
2+
isValidElement,
33
type FC,
4-
type JSXElementConstructor,
4+
type HTMLAttributes,
55
type ReactElement,
66
type ReactNode,
7-
type ReactPortal,
87
} from 'react';
98
import { type CardType, type CareCardType } from './NHSUKTypes';
109

1110
/**
1211
* Assert that a child item is of the given component type.
1312
*/
14-
export const childIsOfComponentType = (
15-
child:
16-
| string
17-
| number
18-
| boolean
19-
| ReactElement<any, string | JSXElementConstructor<any>>
20-
| Iterable<ReactNode>
21-
| ReactPortal
22-
| null
23-
| undefined,
24-
component: FC,
25-
): child is ReactElement<any, string | JSXElementConstructor<any>> | ReactPortal =>
26-
child !== undefined &&
27-
child !== null &&
28-
typeof child === 'object' &&
29-
'type' in child &&
30-
child.type === component;
13+
export const childIsOfComponentType = <T extends HTMLElement, P extends HTMLAttributes<T>>(
14+
child: ReactNode,
15+
component: FC<P>,
16+
): child is ReactElement<P, typeof component> => {
17+
return isValidElement<typeof component>(child) && child.type === component;
18+
};
3119

3220
/**
3321
* Check whether the given card type is that of a care card.

0 commit comments

Comments
 (0)