Unverified Commit 1a81fe44 authored by Joseph's avatar Joseph Committed by GitHub

fix: Do not omit alt on getImgProps return type, ImgProps (#70608)

### What?

Small type tweak, asked for in
https://github.com/vercel/next.js/discussions/70443

### Why?

The `alt` prop is required on the call to `getImgProps`, and the
implementation, returns it back to the caller, however, the type
signature of the return type for `getImgProps`, omits the `alt` prop,
which means that users have to manually apply the `alt` prop.

That is to say, that in this example, tweaked from the docs:

```jsx
import { getImageProps } from 'next/image'
 
export default function Page() {
  const common = { alt: 'Theme Example', width: 800, height: 400 }
  const {
    props: { srcSet: dark },
  } = getImageProps({ ...common, src: '/dark.png' })
  const {
    props: { srcSet: light, ...rest },
  } = getImageProps({ ...common, src: '/light.png' })
 
  return (
    <picture>
      <source media="(prefers-color-scheme: dark)" srcSet={dark} />
      <source media="(prefers-color-scheme: light)" srcSet={light} />
      {/* the alt prop is here, but as far as TypeScript is concerned, it is not */}
      <SomeCustomImageComponent {...rest} /> 
    </picture>
  )
}
``` 

So users have to do:

```jsx
      <SomeCustomImageComponent {...rest} alt={common.alt} /> 
```

See more at, https://github.com/vercel/next.js/discussions/70443

### How?

Remove `alt` from the Omit union.

```tsx
export type ImgProps = Omit<ImageProps, 'src'  | 'alt' | 'loader'> & { /* other stuff */ }
```

Fixes https://github.com/vercel/next.js/discussions/70443
parent 7958c79b
...@@ -69,7 +69,7 @@ export type ImageProps = Omit< ...@@ -69,7 +69,7 @@ export type ImageProps = Omit<
lazyRoot?: string lazyRoot?: string
} }
export type ImgProps = Omit<ImageProps, 'src' | 'alt' | 'loader'> & { export type ImgProps = Omit<ImageProps, 'src' | 'loader'> & {
loading: LoadingValue loading: LoadingValue
width: number | undefined width: number | undefined
height: number | undefined height: number | undefined
......
...@@ -37,6 +37,30 @@ describe('getImageProps()', () => { ...@@ -37,6 +37,30 @@ describe('getImageProps()', () => {
['src', '/_next/image?url=%2Ftest.png&w=256&q=75'], ['src', '/_next/image?url=%2Ftest.png&w=256&q=75'],
]) ])
}) })
it('should have correct type for props', async () => {
const { props } = getImageProps({
alt: 'a nice desc',
id: 'my-image',
src: '/test.png',
width: 100,
height: 200,
})
expect(props.alt).toBeString()
expect(props.id).toBeString()
expect(props.loading).toBeString()
expect(props.width).toBeNumber()
expect(props.height).toBeNumber()
expect(props.decoding).toBeString()
expect(props.style).toBeObject()
expect(props.style.color).toBeString()
expect(props.src).toBeString()
expect(props.srcSet).toBeString()
})
it('should handle priority', async () => { it('should handle priority', async () => {
const { props } = getImageProps({ const { props } = getImageProps({
alt: 'a nice desc', alt: 'a nice desc',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment