Unverified Commit 9857ed1e authored by Rani Malach's avatar Rani Malach Committed by GitHub

fix: support TypeScript `noUncheckedSideEffectImports` for CSS imports (#88199)

## Summary

This PR adds type declarations for plain CSS side-effect imports
(`*.css`, `*.sass`, `*.scss`) to support TypeScript 5.6's
`noUncheckedSideEffectImports` option.

### What?

Added empty module declarations for non-module CSS imports in
`packages/next/types/global.d.ts`:
```ts
declare module '*.css' {}
declare module '*.sass' {}
declare module '*.scss' {}
```

### Why?

When users enable `noUncheckedSideEffectImports` in their tsconfig
(introduced in TypeScript 5.6), they get errors like:
```
Type error: Cannot find module './globals.css' or its corresponding type declarations.
```

This option is becoming increasingly important as it may become the
default in future TypeScript versions.

### How?

Added empty module declarations similar to the existing declarations for
`server-only` and `client-only`.

Fixes #88197

## Checklist

- [x] Related issues linked using `Fixes #88197`
- [x] Tests added - Extended existing
`test/production/typescript-checked-side-effect-imports/` test suite
- [x] Verified the test fails without the fix and passes with the fix

## Test Plan

```bash
pnpm test-start test/production/typescript-checked-side-effect-imports/index.test.ts
```

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 9c5e3da9
......@@ -39,6 +39,13 @@ declare module '*.module.scss' {
export default classes
}
// CSS side-effect imports (non-modules)
// These are needed for `noUncheckedSideEffectImports` support
// See: https://www.typescriptlang.org/tsconfig/#noUncheckedSideEffectImports
declare module '*.css' {}
declare module '*.sass' {}
declare module '*.scss' {}
// We implement the behavior of `import 'server-only'` and `import 'client-only'` on the compiler level
// and thus don't require having them installed as dependencies.
// By default it works fine with typescript, because (surprisingly) TSC *doesn't check side-effecting imports*.
......
import type { ReactNode } from 'react'
import './globals.css'
import './globals.sass'
import './globals.scss'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
......
import { nextTestSetup } from 'e2e-utils'
describe('Imports of server/client-only with noUncheckedSideEffectImports', () => {
describe('Side-effect imports with noUncheckedSideEffectImports', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
dependencies: { sass: '1.54.0' },
skipDeployment: true, // No need to run this in deployment mode.
skipStart: true,
})
......@@ -15,9 +16,12 @@ describe('Imports of server/client-only with noUncheckedSideEffectImports', () =
it('Should build without typescript errors', async () => {
// If something's wrong with our declarations of these modules, TSC will error with:
// `Type error: Cannot find module 'server-only' or its corresponding type declarations.`
// `Type error: Cannot find module '...' or its corresponding type declarations.`
expect(buildResult.cliOutput).not.toContain('server-only')
expect(buildResult.cliOutput).not.toContain('client-only')
expect(buildResult.cliOutput).not.toContain('globals.css')
expect(buildResult.cliOutput).not.toContain('globals.sass')
expect(buildResult.cliOutput).not.toContain('globals.scss')
expect(buildResult.exitCode).toBe(0)
})
})
......@@ -22302,7 +22302,7 @@
},
"test/production/typescript-checked-side-effect-imports/index.test.ts": {
"passed": [
"Imports of server/client-only with noUncheckedSideEffectImports Should build without typescript errors"
"Side-effect imports with noUncheckedSideEffectImports Should build without typescript errors"
],
"failed": [],
"pending": [],
......@@ -19625,7 +19625,7 @@
},
"test/production/typescript-checked-side-effect-imports/index.test.ts": {
"passed": [
"Imports of server/client-only with noUncheckedSideEffectImports Should build without typescript errors"
"Side-effect imports with noUncheckedSideEffectImports Should build without typescript errors"
],
"failed": [],
"pending": [],
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