Skip to content

Conversation

@serhalp
Copy link
Member

@serhalp serhalp commented Oct 21, 2025

This fixes a TypeScript error that occurs when both @netlify/[email protected] and @netlify/functions are installed in a project (with skipLibCheck: false).

It would fail with:

Error: TS2451: Cannot redeclare block-scoped variable 'Netlify'

See for example #489.

You might think it wouldn't be valid to use both those packages at once, but TypeScript global module augmentation is truly global - it doesn't care where you import a package that performs global module augmentation; your whole TS project is compiled/typechecked as one program, unless you explicitly configure things completely separately for your ./netlify/functions vs. ./netlify/edge-functions vs. src/... which pretty much no one does.

TypeScript allows multiple var declarations in a scope to merge, but const declarations are cannot be redeclared. When both packages declare global.Netlify, they must both use var for the declarations to merge successfully.

We were actually already doing this correctly, but unfortunately there was a comment (copypasta from here, where it was accurate) that was lying about why (and there was no type test covering this). This led me to change it to const in #434.

I went down a rabbit hole trying to add a regression type test for this but I had to cut my losses after running into some issues. 😢

This fixes a TypeScript error that occurs when both `@netlify/[email protected]` and
`@netlify/functions` are installed in a project (with `skipLibCheck: false`).

It would fail with:

```
Error: TS2451: Cannot redeclare block-scoped variable 'Netlify'
```

See for example #489.

You would think it wouldn't be valid to use both those packages at once, but TypeScript global
module augmentation is truly _global_ - it doesn't care where you import a package that performs
global module augmentation; your whole TS project is compiled/typechecked as one program, unless you
explicitly configure things completely separately for your `./netlify/functions` vs.
`./netlify/edge-functions` vs. `src/`... which pretty much no one does.

TypeScript allows multiple `var` declarations in a scope to merge, but `const` declarations are
cannot be redeclared. When both packages declare `global.Netlify`, they must both use `var` for the
declarations to merge successfully.

We were actually already doing this correctly, but unfortunately there was a comment that was lying
about _why_ (and there was no type test covering this). This led me to change it to `const` in
#434.

I went down a rabbit hole trying to add a regression type test for this but I had to cut my losses
after running into some issues.
@serhalp serhalp linked an issue Oct 21, 2025 that may be closed by this pull request
@serhalp serhalp marked this pull request as ready for review October 22, 2025 01:22
@serhalp serhalp requested a review from a team as a code owner October 22, 2025 01:22
@serhalp serhalp enabled auto-merge (squash) October 22, 2025 12:22
@serhalp serhalp merged commit bd39b15 into main Oct 22, 2025
44 of 49 checks passed
@serhalp serhalp deleted the fix/489 branch October 22, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type errors in @netlify/edge-functions version 3

3 participants