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

ImageLoader: fix types and remove @ts-expect-error instances #1474

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/blue-moons-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Minor type fixes in the `ImageLoader` and `SvgImage` components
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const SvgImage = (args: StoryArgs): React.ReactElement => {
return (
<ImageLoader
src={svgUrl}
// @ts-expect-error [FEI-5003] - TS2322 - Type 'null' is not assignable to type '() => ReactElement<any, string | JSXElementConstructor<any>> | null | undefined'.
preloader={null}
imgProps={{
alt: "ALT",
Expand All @@ -34,7 +33,6 @@ export const PngImage = (args: StoryArgs): React.ReactElement => {
return (
<ImageLoader
src={imgUrl}
// @ts-expect-error [FEI-5003] - TS2322 - Type 'null' is not assignable to type '() => ReactElement<any, string | JSXElementConstructor<any>> | null | undefined'.
preloader={null}
imgProps={{
alt: "ALT",
Expand All @@ -50,7 +48,6 @@ export const InvalidImageWithChildrenForFailedLoading = (
return (
<ImageLoader
src="http://abcdefiahofshiaof.noway.badimage.com"
// @ts-expect-error [FEI-5003] - TS2322 - Type 'null' is not assignable to type '() => ReactElement<any, string | JSXElementConstructor<any>> | null | undefined'.
preloader={null}
imgProps={{
alt: "ALT",
Expand Down
12 changes: 4 additions & 8 deletions packages/perseus/src/components/image-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ImageProps = {
alt: string;
title?: string;
["aria-hidden"]?: boolean;
tabIndex?: string;
tabIndex?: number;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type error was exposed once I removed the @ts-expect-errors.

onClick?: (e: React.SyntheticEvent) => void;
style?: Dimensions;
};
Expand All @@ -41,7 +41,7 @@ type Props = {
// When the DOM updates to replace the preloader with the image, or
// vice-versa, we trigger this callback.
onUpdate: (status: (typeof Status)[keyof typeof Status]) => void;
preloader: () => React.ReactElement | null | undefined;
preloader: (() => React.ReactNode) | null | undefined;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main fix here is wrapping the function that returns a React element, otherwise it's a function that returns React.ReactElement | null | undefined. I changed the return type to React.ReactNode as that is what a render function returns.

src: string;
};

Expand Down Expand Up @@ -122,17 +122,15 @@ class ImageLoader extends React.Component<Props, State> {

renderImg: () => React.ReactElement<React.ComponentProps<"img">> = () => {
const {src, imgProps} = this.props;
let onKeyUp = null;
let onKeyDown = null;
let onKeyUp;
let onKeyDown;
if (imgProps.onClick != null) {
// @ts-expect-error - TS2322 - Type '(e: React.KeyboardEvent) => void' is not assignable to type 'null'.
onKeyUp = (e: React.KeyboardEvent) => {
// 13 is enter key, 32 is space key
if (e.keyCode === 13 || e.keyCode === 32) {
imgProps.onClick && imgProps.onClick(e);
}
};
// @ts-expect-error - TS2322 - Type '(e: React.KeyboardEvent) => void' is not assignable to type 'null'.
onKeyDown = (e: React.KeyboardEvent) => {
// 32 is space key
if (e.keyCode === 32) {
Expand All @@ -146,9 +144,7 @@ class ImageLoader extends React.Component<Props, State> {
return (
<img
src={staticUrl(src)}
// @ts-expect-error - TS2322 - Type 'null' is not assignable to type 'KeyboardEventHandler<HTMLImageElement> | undefined'.
onKeyUp={onKeyUp}
// @ts-expect-error - TS2322 - Type 'null' is not assignable to type 'KeyboardEventHandler<HTMLImageElement> | undefined'.
onKeyDown={onKeyDown}
{...imgProps}
/>
Expand Down
4 changes: 0 additions & 4 deletions packages/perseus/src/components/svg-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ class SvgImage extends React.Component<Props, State> {
<ImageLoader
src={imageSrc}
imgProps={imageProps}
// @ts-expect-error - TS2322 - Type '(() => Element) | null' is not assignable to type '() => ReactElement<any, string | JSXElementConstructor<any>> | null | undefined'.
preloader={preloader}
onUpdate={this.handleUpdate}
/>
Expand All @@ -679,7 +678,6 @@ class SvgImage extends React.Component<Props, State> {
return (
<ImageLoader
src={imageSrc}
// @ts-expect-error - TS2322 - Type '(() => Element) | null' is not assignable to type '() => ReactElement<any, string | JSXElementConstructor<any>> | null | undefined'.
preloader={preloader}
imgProps={imageProps}
onUpdate={this.handleUpdate}
Expand Down Expand Up @@ -742,7 +740,6 @@ class SvgImage extends React.Component<Props, State> {
src={imageUrl}
onLoad={this.onImageLoad}
onUpdate={this.handleUpdate}
// @ts-expect-error - TS2322 - Type '(() => Element) | null' is not assignable to type '() => ReactElement<any, string | JSXElementConstructor<any>> | null | undefined'.
preloader={preloader}
imgProps={imageProps}
/>
Expand All @@ -758,7 +755,6 @@ class SvgImage extends React.Component<Props, State> {
src={imageUrl}
onLoad={this.onImageLoad}
onUpdate={this.handleUpdate}
// @ts-expect-error - TS2322 - Type '(() => Element) | null' is not assignable to type '() => ReactElement<any, string | JSXElementConstructor<any>> | null | undefined'.
preloader={preloader}
imgProps={imageProps}
/>
Expand Down
1 change: 0 additions & 1 deletion packages/perseus/src/server-item-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ type SerializedState = {
hints: any;
};

/* eslint-disable-next-line react/no-unsafe */
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not actually suppressing anything so I'm removing it.

export class ServerItemRenderer
extends React.Component<Props, State>
implements RendererInterface, KeypadContextRendererInterface
Expand Down