Skip to content

Commit

Permalink
fix(typescript): Fix typing, remove typeguard which would leave props…
Browse files Browse the repository at this point in the history
…: never afterwards
  • Loading branch information
mismosmi committed Sep 21, 2021
1 parent a6050a7 commit a82ef47
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import React from "react";
export type NextImageFromFileProps = Partial<ImageProps> &
Pick<ImageProps, "src">;

function isImageProps(props: NextImageFromFileProps): props is ImageProps {
return typeof props.src !== "string" || !!(props.width && props.height);
}

export default function NextImageFromFile(props: NextImageFromFileProps) {
const [image, setImage] = React.useState<null | {
width: number;
height: number;
}>(null);

if (isImageProps(props)) {
// Fall back to regular next/image if all necessary props are given
if (typeof props.src !== "string" || (props.width && props.height)) {
return <Image {...props} unoptimized />;
}

// No width and height available? Render raw img tag first to figure them out
if (!image)
return (
<img
Expand All @@ -29,6 +27,7 @@ export default function NextImageFromFile(props: NextImageFromFileProps) {
/>
);

// Width and height have been figured out, render the next/image
return (
<Image
{...props}
Expand Down

0 comments on commit a82ef47

Please # to comment.