Skip to content

Commit 2e028b9

Browse files
Copilotserhalp
andauthored
fix: restore missing Netlify global type in @netlify/functions (#434)
* chore: fix existing vitest type tests These weren't asserting anything, and vitest type tests weren't enabled. * build: add missing `skipLibCheck` This is a tough one to explain. This was a latent issue due to a confluence of things: - we don't explicitly do type checking in this repo - instead we rely implicitly on the fact that we use tsup in part to build `.dts` files, which performs type checking - but tsup type checking behaves slightly differently than just running `tsc` in various little ways, largely because it's scoped per entrypoint - we weren't setting `skipLibCheck: true` in the `tsconfig.json`, which seemed reasonable as there were no errors inside `node_modules/` - now when I went to set up vitest type tests, this started type checking some stuff under the hood, and this failed with type errors inside `node_modules/`, despite it using our `tsconfig.json` - it turns out this is because the tsup type checking approach was not encountering these errors; you can confirm this by running `npx --package=typescript tsc --noEmit` in `packages/functions/` So, TL;DR: we had a latent necessity for `skipLibCheck` obscured by our weird implicit type checking via `tsup --dts`. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com> Co-authored-by: Philippe Serhal <philippe.serhal@netlify.com>
1 parent 793b853 commit 2e028b9

File tree

7 files changed

+38
-3
lines changed

7 files changed

+38
-3
lines changed

eslint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ export default tseslint.config(
115115

116116
// Custom assertion functions
117117
'assertNetlifyToml',
118+
119+
// Vitest type testing functions
120+
'expectTypeOf',
118121
],
119122
},
120123
],
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, test, expectTypeOf } from 'vitest'
2+
import type { NetlifyGlobal } from '@netlify/types'
3+
4+
// Import the main module to ensure global augmentation is loaded
5+
import './main.js'
6+
7+
describe('Netlify global type declaration regression test', () => {
8+
test('should augment global scope with `Netlify` global', () => {
9+
expectTypeOf<typeof Netlify>().toEqualTypeOf<NetlifyGlobal>()
10+
expectTypeOf<typeof Netlify>().toHaveProperty('env')
11+
expectTypeOf<typeof Netlify>().toHaveProperty('context')
12+
})
13+
})

packages/functions/src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import type { NetlifyGlobal } from '@netlify/types'
2+
3+
declare global {
4+
const Netlify: NetlifyGlobal
5+
}
6+
17
export { builder } from './lib/builder.js'
28
export { purgeCache } from './lib/purge_cache.js'
39
export { schedule } from './lib/schedule.js'

packages/functions/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"removeComments": false,
1212
"strict": true,
1313
"moduleResolution": "node",
14+
"skipLibCheck": true,
1415
"forceConsistentCasingInFileNames": true
1516
},
1617
"include": ["src", "dev"]

packages/functions/vitest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ export default defineConfig({
77
test: {
88
include: ['(src|dev)/**/*.test.ts'],
99
testTimeout: 30_000,
10+
typecheck: {
11+
enabled: true,
12+
},
1013
},
1114
})

packages/types/src/main.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { fileURLToPath } from 'node:url'
44

55
import { expect, expectTypeOf, test } from 'vitest'
66

7-
import { Context, NetlifyGlobal } from './main.js'
7+
import type { Context, NetlifyGlobal } from './main.js'
88
import * as main from './main.js'
99

1010
test('Exports types', () => {
11-
expectTypeOf<Context>()
12-
expectTypeOf<NetlifyGlobal>()
11+
expectTypeOf<Context>().toExtend<object>()
12+
expectTypeOf<NetlifyGlobal>().toExtend<object>()
1313
})
1414

1515
test('Does not export runtime code', () => {

packages/types/vitest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
typecheck: {
6+
enabled: true,
7+
},
8+
},
9+
})

0 commit comments

Comments
 (0)