Skip to content

Commit cc39e55

Browse files
feat(types): add more precise typing for known query types to match known as types (#21863)
Co-authored-by: nathanhleung <nathanhleung@users.noreply.github.com> Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent eb12604 commit cc39e55

3 files changed

Lines changed: 146 additions & 6 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
import type { Equal, ExpectTrue } from '@type-challenges/utils'
3+
import type { ImportGlobFunction } from '#types/importGlob'
4+
5+
const importGlobFunction: ImportGlobFunction = () => ({})
6+
7+
const importGlobEagerReturn = importGlobFunction('', {
8+
eager: true,
9+
})
10+
11+
const importGlobNonEagerReturn = importGlobFunction('', {
12+
eager: false,
13+
})
14+
15+
const importGlobEagerReturnWithKnownQuery = importGlobFunction('', {
16+
eager: true,
17+
query: '?raw',
18+
})
19+
20+
const importGlobNonEagerReturnWithKnownQuery = importGlobFunction('', {
21+
eager: false,
22+
query: '?raw',
23+
})
24+
25+
const importGlobEagerReturnWithUnknownQuery = importGlobFunction('', {
26+
eager: true,
27+
query: '?unknown',
28+
})
29+
30+
const importGlobNonEagerReturnWithUnknownQuery = importGlobFunction('', {
31+
eager: false,
32+
query: '?unknown',
33+
})
34+
35+
const importGlobEagerReturnWithKnownAs = importGlobFunction('', {
36+
eager: true,
37+
as: 'url',
38+
})
39+
40+
const importGlobNonEagerReturnWithKnownAs = importGlobFunction('', {
41+
eager: false,
42+
as: 'url',
43+
})
44+
45+
const importGlobEagerReturnWithWorkerAs = importGlobFunction('', {
46+
eager: true,
47+
as: 'worker',
48+
})
49+
50+
const importGlobNonEagerReturnWithWorkerAs = importGlobFunction('', {
51+
eager: false,
52+
as: 'worker',
53+
})
54+
55+
export type cases = [
56+
ExpectTrue<Equal<typeof importGlobEagerReturn, Record<string, unknown>>>,
57+
ExpectTrue<
58+
Equal<
59+
typeof importGlobNonEagerReturn,
60+
Record<string, () => Promise<unknown>>
61+
>
62+
>,
63+
ExpectTrue<
64+
Equal<typeof importGlobEagerReturnWithKnownQuery, Record<string, string>>
65+
>,
66+
ExpectTrue<
67+
Equal<
68+
typeof importGlobNonEagerReturnWithKnownQuery,
69+
Record<string, () => Promise<string>>
70+
>
71+
>,
72+
ExpectTrue<
73+
Equal<typeof importGlobEagerReturnWithUnknownQuery, Record<string, unknown>>
74+
>,
75+
ExpectTrue<
76+
Equal<
77+
typeof importGlobNonEagerReturnWithUnknownQuery,
78+
Record<string, () => Promise<unknown>>
79+
>
80+
>,
81+
ExpectTrue<
82+
Equal<typeof importGlobEagerReturnWithKnownAs, Record<string, string>>
83+
>,
84+
ExpectTrue<
85+
Equal<
86+
typeof importGlobNonEagerReturnWithKnownAs,
87+
Record<string, () => Promise<string>>
88+
>
89+
>,
90+
ExpectTrue<
91+
Equal<typeof importGlobEagerReturnWithWorkerAs, Record<string, Worker>>
92+
>,
93+
ExpectTrue<
94+
Equal<
95+
typeof importGlobNonEagerReturnWithWorkerAs,
96+
Record<string, () => Promise<Worker>>
97+
>
98+
>,
99+
]
100+
101+
export {}

packages/vite/src/node/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ export type {
269269
ImportGlobOptions,
270270
GeneralImportGlobOptions,
271271
KnownAsTypeMap,
272+
KnownQueryTypeMap,
272273
} from '#types/importGlob'
273274
export type { ChunkMetadata, CustomPluginOptionsVite } from '#types/metadata'
274275

packages/vite/types/importGlob.d.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
// make input suggestions work
2+
type StringQueryType = `?${keyof KnownAsTypeMap}` | (string & {})
3+
type BaseQueryType = StringQueryType | Record<string, string | number | boolean>
4+
15
export interface ImportGlobOptions<
26
Eager extends boolean,
37
AsType extends string,
8+
QueryType extends BaseQueryType,
49
> {
510
/**
611
* Import type for the import url.
@@ -21,7 +26,7 @@ export interface ImportGlobOptions<
2126
/**
2227
* Custom queries
2328
*/
24-
query?: string | Record<string, string | number | boolean>
29+
query?: QueryType
2530
/**
2631
* Search files also inside `node_modules/` and hidden directories (e.g. `.git/`). This might have impact on performance.
2732
*
@@ -34,7 +39,18 @@ export interface ImportGlobOptions<
3439
base?: string
3540
}
3641

37-
export type GeneralImportGlobOptions = ImportGlobOptions<boolean, string>
42+
export type ImportGlobOptionsWithoutAs<
43+
Eager extends boolean,
44+
QueryType extends BaseQueryType,
45+
> = Omit<ImportGlobOptions<Eager, string, QueryType>, 'as'> & {
46+
as?: never
47+
}
48+
49+
export type GeneralImportGlobOptions = ImportGlobOptions<
50+
boolean,
51+
string,
52+
BaseQueryType
53+
>
3854

3955
/**
4056
* Declare Worker in case DOM is not added to the tsconfig lib causing
@@ -52,19 +68,41 @@ export interface KnownAsTypeMap {
5268
worker: Worker
5369
}
5470

71+
type KnownQueryTypeMap = {
72+
[K in keyof KnownAsTypeMap as `?${K}`]: KnownAsTypeMap[K]
73+
}
74+
5575
export interface ImportGlobFunction {
5676
/**
5777
* Import a list of files with a glob pattern.
5878
*
59-
* Overload 1: No generic provided, infer the type from `eager` and `as`
79+
* Overload 1A: No generic provided, infer the type from `eager` and `query`
80+
*/
81+
<
82+
Eager extends boolean,
83+
Query extends BaseQueryType,
84+
T = Query extends keyof KnownQueryTypeMap
85+
? KnownQueryTypeMap[Query]
86+
: unknown,
87+
>(
88+
glob: string | string[],
89+
options?: ImportGlobOptionsWithoutAs<Eager, Query>,
90+
): (Eager extends true ? true : false) extends true
91+
? Record<string, T>
92+
: Record<string, () => Promise<T>>
93+
/**
94+
* Import a list of files with a glob pattern.
95+
*
96+
* Overload 1B: No generic provided, infer the type from `eager` and `as`
97+
* (deprecated, use `query` instead)
6098
*/
6199
<
62100
Eager extends boolean,
63101
As extends string,
64102
T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown,
65103
>(
66104
glob: string | string[],
67-
options?: ImportGlobOptions<Eager, As>,
105+
options?: ImportGlobOptions<Eager, As, BaseQueryType>,
68106
): (Eager extends true ? true : false) extends true
69107
? Record<string, T>
70108
: Record<string, () => Promise<T>>
@@ -75,7 +113,7 @@ export interface ImportGlobFunction {
75113
*/
76114
<M>(
77115
glob: string | string[],
78-
options?: ImportGlobOptions<false, string>,
116+
options?: ImportGlobOptions<false, string, BaseQueryType>,
79117
): Record<string, () => Promise<M>>
80118
/**
81119
* Import a list of files with a glob pattern.
@@ -84,6 +122,6 @@ export interface ImportGlobFunction {
84122
*/
85123
<M>(
86124
glob: string | string[],
87-
options: ImportGlobOptions<true, string>,
125+
options: ImportGlobOptions<true, string, BaseQueryType>,
88126
): Record<string, M>
89127
}

0 commit comments

Comments
 (0)