sanity-io / image-url

Tools to generate image urls from Sanity content
https://www.sanity.io/docs/presenting-images#mY9Be3Ph
MIT License
67 stars 23 forks source link

Export the ImageUrlBuilder type #5

Closed Svish closed 4 years ago

Svish commented 4 years ago

Wanted to accept an ImageUrlBuilder as a prop in a component, but the type isn't exported, as far as I can see. 😟

Like, I can "see" it via my IDE, but since it's not exported, I can't actually use it as a prop or function parameter type. 😕

Please export the ImageUrlBuilder type?

Svish commented 4 years ago

Worked around it as follows, but really shouldn't be necessary working around this. Should just be an exported type from the library.

import imageUrlBuilder from '@sanity/image-url';
type ImageUrlBuilder = ReturnType<typeof imageUrlBuilder>;
rexxars commented 4 years ago

Could you see if 0.140.16 fixes the issue?

Svish commented 4 years ago

@rexxars Looking at the diff for that version I assume it will, but checking now (if only my slow connection could hurry up) to be sure. Will close the issue when verified. 👍

Svish commented 4 years ago

Yep, I can use the type in my code now:

import React from 'react';
import { ImageUrlBuilder } from '@sanity/image-url/lib/types/builder';

interface ImageProps extends Omit<React.HTMLProps<HTMLImageElement>, 'src'> {
  src: string | ImageUrlBuilder;
}

export default function Image(props: ImageProps) {
  if (typeof props.src !== 'string') {
    props.src = props.src.url()!;
  }

  // ...
}

👍