Skip to content

Commit

Permalink
feat(typescript): prop type is Partial<ImageProps>
Browse files Browse the repository at this point in the history
  • Loading branch information
mismosmi committed Sep 21, 2021
1 parent 27cd51f commit a6050a7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import Image, { ImageProps } from "next/image";
import React from "react";

export default function NextImageFromFile(props: ImageProps) {
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 (typeof props.src !== "string" || (props.width && props.height)) {
if (isImageProps(props)) {
return <Image {...props} unoptimized />;
}

Expand Down

0 comments on commit a6050a7

Please # to comment.