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

Fix types for static image #25808

Merged
merged 6 commits into from
Jun 7, 2021
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
44 changes: 28 additions & 16 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,45 @@ function isStaticImport(src: string | StaticImport): src is StaticImport {
)
}

type StringImageProps = {
src: string
} & (
| { width?: never; height?: never; layout: 'fill' }
| {
width: number | string
height: number | string
layout?: Exclude<LayoutValue, 'fill'>
}
) &
(
| {
placeholder?: Exclude<PlaceholderValue, 'blur'>
blurDataURL?: never
}
| { placeholder: 'blur'; blurDataURL: string }
)

type ObjectImageProps = {
src: StaticImport
width?: number | string
height?: number | string
layout?: LayoutValue
placeholder?: PlaceholderValue
blurDataURL?: never
}

export type ImageProps = Omit<
JSX.IntrinsicElements['img'],
'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading' | 'style'
> & {
src: string | StaticImport
loader?: ImageLoader
quality?: number | string
priority?: boolean
loading?: LoadingValue
unoptimized?: boolean
objectFit?: ImgElementStyle['objectFit']
objectPosition?: ImgElementStyle['objectPosition']
} & (
| { width?: never; height?: never; layout: 'fill' }
| {
width: number | string
height: number | string
layout?: Exclude<LayoutValue, 'fill'>
}
) &
(
| {
placeholder?: Exclude<PlaceholderValue, 'blur'>
blurDataURL?: never
}
| { placeholder: 'blur'; blurDataURL: string }
)
} & (StringImageProps | ObjectImageProps)

const {
deviceSizes: configDeviceSizes,
Expand Down
55 changes: 55 additions & 0 deletions packages/next/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,58 @@ declare module '*.module.scss' {
const classes: { readonly [key: string]: string }
export default classes
}

interface StaticImageData {
src: string
height: number
width: number
placeholder?: string
}

declare module '*.png' {
const content: StaticImageData

export default content
}

declare module '*.svg' {
const content: StaticImageData

export default content
}

declare module '*.jpg' {
const content: StaticImageData

export default content
}

declare module '*.jpeg' {
const content: StaticImageData

export default content
}

declare module '*.gif' {
const content: StaticImageData

export default content
}

declare module '*.webp' {
const content: StaticImageData

export default content
}

declare module '*.ico' {
const content: StaticImageData

export default content
}

declare module '*.bmp' {
const content: StaticImageData

export default content
}
7 changes: 7 additions & 0 deletions test/integration/image-component/typescript/pages/invalid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const Invalid = () => {
id="no-width-or-height"
src="https://via.placeholder.com/500"
></Image>
<Image
id="no-blur-data-url"
ijjk marked this conversation as resolved.
Show resolved Hide resolved
src="https://via.placeholder.com/500"
width={500}
height={500}
placeholder="blur"
></Image>
<p id="stubtext">This is the invalid usage</p>
</div>
)
Expand Down
15 changes: 15 additions & 0 deletions test/integration/image-component/typescript/pages/valid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import Image from 'next/image'
import testTall from '../public/tall.png'

const Page = () => {
return (
Expand Down Expand Up @@ -58,6 +59,20 @@ const Page = () => {
objectFit="scale-down"
objectPosition="50px"
/>
<Image
id="placeholder-and-blur-data-url"
src="https://via.placeholder.com/500"
width={500}
height={500}
placeholder="blur"
blurDataURL="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
/>
<Image id="no-width-and-height" src={testTall} />
<Image
id="object-src-with-placeholder"
src={testTall}
placeholder="blur"
/>
<p id="stubtext">This is valid usage of the Image component</p>
</div>
)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const runTests = () => {

await browser.back().waitForElementByCss('#another')

expect(await browser.elementByCss('#another').text()).toBe('another page')
expect(await browser.waitForElementByCss('#another').text()).toBe(
'another page'
)

expect(await browser.eval('window.beforeNav')).toBe(1)
})
Expand Down