Skip to content

feat(types): add more precise typing for known query types to match known as types#21863

Merged
sapphi-red merged 5 commits into
vitejs:mainfrom
nathanhleung:nathanhleung/import-glob-types
Jun 3, 2026
Merged

feat(types): add more precise typing for known query types to match known as types#21863
sapphi-red merged 5 commits into
vitejs:mainfrom
nathanhleung:nathanhleung/import-glob-types

Conversation

@nathanhleung

Copy link
Copy Markdown
Contributor

Summary

In Vite 5.1 (released Feb 8, 2024), the as option in the import.meta.glob function was deprecated. The official recommendation was to switch to the query option, e.g.:

const myGlobImports = import.meta.glob("./path/*.csv", {
-  as: "raw",
+  query: "?raw",
  eager: true,
  import: "default",
});

Unfortunately, this loses some type information; specifically, TypeScript infers myGlobImports as Record<string, unknown> instead of Record<string, string> (or a Record pointing to Promises returning unknown or string, respectively, if eager is false) after making this change. The reason for this is because importGlob.d.ts does not have an overload which infers the type of the import from query when possible; there is only an overload which infers the type from as.

Changes

This PR adds an overload in importGlob.d.ts that matches the logic of the existing as overload but works with query, allowing users to migrate from as to query when using import.meta.glob without losing type information (the current workaround is unsafely casting with as unknown as Record<string, string>).

Tests?

I've applied this patch to an internal project and it seems to be working; I've tested with eager both true and false and swapping between as and query (the new overload needs to go above the previous overload because it is more specific and TypeScript applies overloads top-to-bottom).

I didn't see a way to test these types other than running pnpm build (which runs pnpm build-types internally) — for what it's worth, the local build did complete without errors. Let me know if there is anything else I can do to test.

@sapphi-red sapphi-red left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can have this.

Would you add type tests like these?

ExpectTrue<Equal<typeof useDefaultTypeForUndefined1, { foo: number }>>,

@sapphi-red sapphi-red added the p2-to-be-discussed Enhancement under consideration (priority) label Mar 16, 2026
@github-project-automation github-project-automation Bot moved this to Discussing in Team Board Mar 16, 2026
@sapphi-red sapphi-red moved this from Discussing to P2 - 3 in Team Board Mar 16, 2026
@nathanhleung

nathanhleung commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

Would you add type tests like these?

ExpectTrue<Equal<typeof useDefaultTypeForUndefined1, { foo: number }>>,

I created a new file importGlob.ts in the same directory and added some tests there, including coverage for the existing as overload. If these tests should be placed in another location, let me know and I can move.

@nathanhleung nathanhleung requested a review from sapphi-red March 17, 2026 03:24
@nathanhleung

Copy link
Copy Markdown
Contributor Author

Friendly ping here, anything else I could do on my side to help move this along? cc @sapphi-red

@sapphi-red sapphi-red moved this from P2 - 3 to Approved in Team Board Apr 29, 2026
@sapphi-red sapphi-red added p2-nice-to-have Not breaking anything but nice to have (priority) and removed p2-to-be-discussed Enhancement under consideration (priority) labels Jun 3, 2026
@sapphi-red

Copy link
Copy Markdown
Member

/ecosystem-ci run

@pkg-pr-new

pkg-pr-new Bot commented Jun 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vitejs/plugin-legacy

pnpm add https://pkg.pr.new/@vitejs/plugin-legacy@21863 -D
npm i https://pkg.pr.new/@vitejs/plugin-legacy@21863 -D
yarn add https://pkg.pr.new/@vitejs/plugin-legacy@21863.tgz -D

vite

pnpm add https://pkg.pr.new/vite@21863 -D
npm i https://pkg.pr.new/vite@21863 -D
yarn add https://pkg.pr.new/vite@21863.tgz -D

commit: a54925b

@vite-ecosystem-ci

Copy link
Copy Markdown

@sapphi-red sapphi-red merged commit cc39e55 into vitejs:main Jun 3, 2026
19 of 20 checks passed
@nathanhleung nathanhleung deleted the nathanhleung/import-glob-types branch June 3, 2026 17:49
wan9chi added a commit to voidzero-dev/vite-plus that referenced this pull request Jun 7, 2026
…nvs)

Vite reports its inputs/outputs/envs to the `vp` runner via
`@voidzero-dev/vite-task-client`, so `vp build` no longer needs a
hand-synthesized cache config.

- Bump vite-task git deps to c44d7c33 (runner-aware IPC server/client +
  embedded napi addon); regenerate run-config.ts for the new task-config types.
- Point the vite behind vp build/vp dev to latest vitejs/vite (c13a37b53),
  which carries the vite-task-client integration. vite lists vite-task-client
  in devDependencies, so it is bundled into vite's output (and thus into core)
  the same way as vite's other bundled deps (cac, esbuild, ...) — no extra
  dependency or build wiring needed.
- Work around an upstream vite dts bug (vitejs/vite#21863): `node/index.ts`
  re-exports `KnownQueryTypeMap` but the type is declared without `export`,
  which breaks downstream type bundling (`vp build` MISSING_EXPORT). core's
  build adds the missing `export` when copying vite's type defs.
- Remove vp build's synthetic cache config (env: [VITE_*], manual input
  excludes). All-None EnabledCacheConfig keeps caching enabled with auto
  input/output inference (output restoration); vp dev stays disabled.
- Snap tests: vp-build-auto-output-restoration, vp-build-auto-tracked-env.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
wan9chi added a commit to voidzero-dev/vite-plus that referenced this pull request Jun 15, 2026
…nvs)

Vite reports its inputs/outputs/envs to the `vp` runner via
`@voidzero-dev/vite-task-client`, so `vp build` no longer needs a
hand-synthesized cache config.

- Bump vite-task git deps to c44d7c33 (runner-aware IPC server/client +
  embedded napi addon); regenerate run-config.ts for the new task-config types.
- Point the vite behind vp build/vp dev to latest vitejs/vite (c13a37b53),
  which carries the vite-task-client integration. vite lists vite-task-client
  in devDependencies, so it is bundled into vite's output (and thus into core)
  the same way as vite's other bundled deps (cac, esbuild, ...) — no extra
  dependency or build wiring needed.
- Work around an upstream vite dts bug (vitejs/vite#21863): `node/index.ts`
  re-exports `KnownQueryTypeMap` but the type is declared without `export`,
  which breaks downstream type bundling (`vp build` MISSING_EXPORT). core's
  build adds the missing `export` when copying vite's type defs.
- Remove vp build's synthetic cache config (env: [VITE_*], manual input
  excludes). All-None EnabledCacheConfig keeps caching enabled with auto
  input/output inference (output restoration); vp dev stays disabled.
- Snap tests: vp-build-auto-output-restoration, vp-build-auto-tracked-env.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
wan9chi added a commit to voidzero-dev/vite-plus that referenced this pull request Jun 21, 2026
…nvs)

Vite reports its inputs/outputs/envs to the `vp` runner via
`@voidzero-dev/vite-task-client`, so `vp build` no longer needs a
hand-synthesized cache config.

- Bump vite-task git deps to c44d7c33 (runner-aware IPC server/client +
  embedded napi addon); regenerate run-config.ts for the new task-config types.
- Point the vite behind vp build/vp dev to latest vitejs/vite (c13a37b53),
  which carries the vite-task-client integration. vite lists vite-task-client
  in devDependencies, so it is bundled into vite's output (and thus into core)
  the same way as vite's other bundled deps (cac, esbuild, ...) — no extra
  dependency or build wiring needed.
- Work around an upstream vite dts bug (vitejs/vite#21863): `node/index.ts`
  re-exports `KnownQueryTypeMap` but the type is declared without `export`,
  which breaks downstream type bundling (`vp build` MISSING_EXPORT). core's
  build adds the missing `export` when copying vite's type defs.
- Remove vp build's synthetic cache config (env: [VITE_*], manual input
  excludes). All-None EnabledCacheConfig keeps caching enabled with auto
  input/output inference (output restoration); vp dev stays disabled.
- Snap tests: vp-build-auto-output-restoration, vp-build-auto-tracked-env.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
wan9chi added a commit to voidzero-dev/vite-plus that referenced this pull request Jun 21, 2026
…nvs)

Vite reports its inputs/outputs/envs to the `vp` runner via
`@voidzero-dev/vite-task-client`, so `vp build` no longer needs a
hand-synthesized cache config.

- Bump vite-task git deps to c44d7c33 (runner-aware IPC server/client +
  embedded napi addon); regenerate run-config.ts for the new task-config types.
- Point the vite behind vp build/vp dev to latest vitejs/vite (c13a37b53),
  which carries the vite-task-client integration. vite lists vite-task-client
  in devDependencies, so it is bundled into vite's output (and thus into core)
  the same way as vite's other bundled deps (cac, esbuild, ...) — no extra
  dependency or build wiring needed.
- Work around an upstream vite dts bug (vitejs/vite#21863): `node/index.ts`
  re-exports `KnownQueryTypeMap` but the type is declared without `export`,
  which breaks downstream type bundling (`vp build` MISSING_EXPORT). core's
  build adds the missing `export` when copying vite's type defs.
- Remove vp build's synthetic cache config (env: [VITE_*], manual input
  excludes). All-None EnabledCacheConfig keeps caching enabled with auto
  input/output inference (output restoration); vp dev stays disabled.
- Snap tests: vp-build-auto-output-restoration, vp-build-auto-tracked-env.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
wan9chi added a commit to voidzero-dev/vite-plus that referenced this pull request Jun 23, 2026
…nvs)

Vite reports its inputs/outputs/envs to the `vp` runner via
`@voidzero-dev/vite-task-client`, so `vp build` no longer needs a
hand-synthesized cache config.

- Bump vite-task git deps to c44d7c33 (runner-aware IPC server/client +
  embedded napi addon); regenerate run-config.ts for the new task-config types.
- Point the vite behind vp build/vp dev to latest vitejs/vite (c13a37b53),
  which carries the vite-task-client integration. vite lists vite-task-client
  in devDependencies, so it is bundled into vite's output (and thus into core)
  the same way as vite's other bundled deps (cac, esbuild, ...) — no extra
  dependency or build wiring needed.
- Work around an upstream vite dts bug (vitejs/vite#21863): `node/index.ts`
  re-exports `KnownQueryTypeMap` but the type is declared without `export`,
  which breaks downstream type bundling (`vp build` MISSING_EXPORT). core's
  build adds the missing `export` when copying vite's type defs.
- Remove vp build's synthetic cache config (env: [VITE_*], manual input
  excludes). All-None EnabledCacheConfig keeps caching enabled with auto
  input/output inference (output restoration); vp dev stays disabled.
- Snap tests: vp-build-auto-output-restoration, vp-build-auto-tracked-env.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
wan9chi added a commit to voidzero-dev/vite-plus that referenced this pull request Jun 23, 2026
…nvs)

Vite reports its inputs/outputs/envs to the `vp` runner via
`@voidzero-dev/vite-task-client`, so `vp build` no longer needs a
hand-synthesized cache config.

- Bump vite-task git deps to c44d7c33 (runner-aware IPC server/client +
  embedded napi addon); regenerate run-config.ts for the new task-config types.
- Point the vite behind vp build/vp dev to latest vitejs/vite (c13a37b53),
  which carries the vite-task-client integration. vite lists vite-task-client
  in devDependencies, so it is bundled into vite's output (and thus into core)
  the same way as vite's other bundled deps (cac, esbuild, ...) — no extra
  dependency or build wiring needed.
- Work around an upstream vite dts bug (vitejs/vite#21863): `node/index.ts`
  re-exports `KnownQueryTypeMap` but the type is declared without `export`,
  which breaks downstream type bundling (`vp build` MISSING_EXPORT). core's
  build adds the missing `export` when copying vite's type defs.
- Remove vp build's synthetic cache config (env: [VITE_*], manual input
  excludes). All-None EnabledCacheConfig keeps caching enabled with auto
  input/output inference (output restoration); vp dev stays disabled.
- Snap tests: vp-build-auto-output-restoration, vp-build-auto-tracked-env.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2-nice-to-have Not breaking anything but nice to have (priority) trigger: preview

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants