|
1 | 1 | import { getSlotsPropertyName } from '../utils/shared'; |
2 | | -import { endOfLine, newLine } from './common'; |
3 | 2 |
|
4 | | -export function generateGlobalTypes(mode: 'global' | 'local', lib: string, target: number, strictTemplates: boolean) { |
| 3 | +export function generateGlobalTypes(lib: string, target: number, strictTemplates: boolean) { |
5 | 4 | const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${strictTemplates ? '' : ' & Record<string, unknown>'}`; |
| 5 | + return ` |
| 6 | +; declare module '${lib}' { |
| 7 | + export interface GlobalComponents { } |
| 8 | +} |
| 9 | +declare global { |
| 10 | + const __VLS_intrinsicElements: __VLS_IntrinsicElements; |
| 11 | + const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any }; |
| 12 | + const __VLS_unref: typeof import('${lib}').unref; |
6 | 13 |
|
7 | | - let decl = ''; |
8 | | - let str = ''; |
9 | | - let globalComponentsType: string; |
| 14 | + const __VLS_nativeElements = { |
| 15 | + ...{} as SVGElementTagNameMap, |
| 16 | + ...{} as HTMLElementTagNameMap, |
| 17 | + }; |
10 | 18 |
|
11 | | - if (mode === 'global') { |
12 | | - str += `// @ts-nocheck${newLine}`; |
13 | | - str += `export {}${endOfLine}`; |
14 | | - str += `declare module '${lib}' {${newLine}`; |
15 | | - str += ` export interface GlobalComponents { }${newLine}`; |
16 | | - str += `}${newLine}`; |
17 | | - str += `declare global {${newLine}`; |
18 | | - globalComponentsType = `import('${lib}').GlobalComponents`; |
19 | | - } |
20 | | - else { |
21 | | - decl = 'declare '; |
22 | | - str += `// @ts-ignore${newLine}`; |
23 | | - str += `const __VLS_globalComponents = { ...{} as import('${lib}').GlobalComponents }${endOfLine}`; |
24 | | - globalComponentsType = `void extends typeof __VLS_globalComponents ? {} : typeof __VLS_globalComponents`; |
25 | | - } |
26 | | - |
27 | | - str += ` |
28 | | -${decl}const __VLS_intrinsicElements: __VLS_IntrinsicElements; |
29 | | -${decl}const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any }; |
30 | | -${decl}const __VLS_unref: typeof import('${lib}').unref; |
31 | | -
|
32 | | -const __VLS_nativeElements = { |
33 | | - ...{} as SVGElementTagNameMap, |
34 | | - ...{} as HTMLElementTagNameMap, |
35 | | -}; |
36 | | -
|
37 | | -type __VLS_IntrinsicElements = ${( |
| 19 | + type __VLS_IntrinsicElements = ${( |
38 | 20 | target >= 3.3 |
39 | 21 | ? `import('${lib}/jsx-runtime').JSX.IntrinsicElements;` |
40 | 22 | : `globalThis.JSX.IntrinsicElements;` |
41 | 23 | )} |
42 | | -type __VLS_Element = ${( |
| 24 | + type __VLS_Element = ${( |
43 | 25 | target >= 3.3 |
44 | 26 | ? `import('${lib}/jsx-runtime').JSX.Element;` |
45 | 27 | : `globalThis.JSX.Element;` |
46 | 28 | )} |
47 | | -type __VLS_GlobalComponents = ${( |
| 29 | + type __VLS_GlobalComponents = ${( |
48 | 30 | target >= 3.5 |
49 | | - ? globalComponentsType |
50 | | - : `(${globalComponentsType}) & Pick<typeof import('${lib}'), 'Transition' | 'TransitionGroup' | 'KeepAlive' | 'Suspense' | 'Teleport'>;` |
| 31 | + ? `import('${lib}').GlobalComponents` |
| 32 | + : `import('${lib}').GlobalComponents & Pick<typeof import('${lib}'), 'Transition' | 'TransitionGroup' | 'KeepAlive' | 'Suspense' | 'Teleport'>;` |
51 | 33 | )} |
52 | | -type __VLS_IsAny<T> = 0 extends 1 & T ? true : false; |
53 | | -type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A; |
54 | | -type __VLS_unknownDirective = (arg1: unknown, arg2: unknown, arg3: unknown, arg4: unknown) => void; |
55 | | -type __VLS_WithComponent<N0 extends string, LocalComponents, N1 extends string, N2 extends string, N3 extends string> = |
56 | | - N1 extends keyof LocalComponents ? N1 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N1] } : |
57 | | - N2 extends keyof LocalComponents ? N2 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N2] } : |
58 | | - N3 extends keyof LocalComponents ? N3 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N3] } : |
59 | | - N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } : |
60 | | - N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } : |
61 | | - N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } : |
62 | | - ${strictTemplates ? '{}' : '{ [K in N0]: unknown }'} |
63 | | -type __VLS_FunctionalComponentProps<T, K> = |
64 | | - '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: { props?: infer P } } ? NonNullable<P> : never |
65 | | - : T extends (props: infer P, ...args: any) => any ? P : |
66 | | - {}; |
67 | | -type __VLS_IsFunction<T, K> = K extends keyof T |
68 | | - ? __VLS_IsAny<T[K]> extends false |
69 | | - ? unknown extends T[K] |
70 | | - ? false |
71 | | - : true |
72 | | - : false |
73 | | - : false; |
74 | | -// fix https://github.com/vuejs/language-tools/issues/926 |
75 | | -type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never; |
76 | | -type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R |
77 | | - ? U extends T |
78 | | - ? never |
79 | | - : __VLS_OverloadUnionInner<T, Pick<T, keyof T> & U & ((...args: A) => R)> | ((...args: A) => R) |
80 | | - : never; |
81 | | -type __VLS_OverloadUnion<T> = Exclude< |
82 | | - __VLS_OverloadUnionInner<(() => never) & T>, |
83 | | - T extends () => never ? never : () => never |
84 | | ->; |
85 | | -type __VLS_ConstructorOverloads<T> = __VLS_OverloadUnion<T> extends infer F |
86 | | - ? F extends (event: infer E, ...args: infer A) => any |
87 | | - ? { [K in E & string]: (...args: A) => void; } |
88 | | - : never |
89 | | - : never; |
90 | | -type __VLS_NormalizeEmits<T> = __VLS_PrettifyGlobal< |
91 | | - __VLS_UnionToIntersection< |
92 | | - __VLS_ConstructorOverloads<T> & { |
93 | | - [K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never |
94 | | - } |
95 | | - > |
96 | | ->; |
97 | | -type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {}; |
| 34 | + type __VLS_IsAny<T> = 0 extends 1 & T ? true : false; |
| 35 | + type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A; |
| 36 | + type __VLS_unknownDirective = (arg1: unknown, arg2: unknown, arg3: unknown, arg4: unknown) => void; |
| 37 | + type __VLS_WithComponent<N0 extends string, LocalComponents, N1 extends string, N2 extends string, N3 extends string> = |
| 38 | + N1 extends keyof LocalComponents ? N1 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N1] } : |
| 39 | + N2 extends keyof LocalComponents ? N2 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N2] } : |
| 40 | + N3 extends keyof LocalComponents ? N3 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N3] } : |
| 41 | + N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } : |
| 42 | + N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } : |
| 43 | + N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } : |
| 44 | + ${strictTemplates ? '{}' : '{ [K in N0]: unknown }'} |
| 45 | + type __VLS_FunctionalComponentProps<T, K> = |
| 46 | + '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: { props?: infer P } } ? NonNullable<P> : never |
| 47 | + : T extends (props: infer P, ...args: any) => any ? P : |
| 48 | + {}; |
| 49 | + type __VLS_IsFunction<T, K> = K extends keyof T |
| 50 | + ? __VLS_IsAny<T[K]> extends false |
| 51 | + ? unknown extends T[K] |
| 52 | + ? false |
| 53 | + : true |
| 54 | + : false |
| 55 | + : false; |
| 56 | + // fix https://github.com/vuejs/language-tools/issues/926 |
| 57 | + type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never; |
| 58 | + type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R |
| 59 | + ? U extends T |
| 60 | + ? never |
| 61 | + : __VLS_OverloadUnionInner<T, Pick<T, keyof T> & U & ((...args: A) => R)> | ((...args: A) => R) |
| 62 | + : never; |
| 63 | + type __VLS_OverloadUnion<T> = Exclude< |
| 64 | + __VLS_OverloadUnionInner<(() => never) & T>, |
| 65 | + T extends () => never ? never : () => never |
| 66 | + >; |
| 67 | + type __VLS_ConstructorOverloads<T> = __VLS_OverloadUnion<T> extends infer F |
| 68 | + ? F extends (event: infer E, ...args: infer A) => any |
| 69 | + ? { [K in E & string]: (...args: A) => void; } |
| 70 | + : never |
| 71 | + : never; |
| 72 | + type __VLS_NormalizeEmits<T> = __VLS_PrettifyGlobal< |
| 73 | + __VLS_UnionToIntersection< |
| 74 | + __VLS_ConstructorOverloads<T> & { |
| 75 | + [K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never |
| 76 | + } |
| 77 | + > |
| 78 | + >; |
| 79 | + type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {}; |
98 | 80 |
|
99 | | -${decl}function __VLS_getVForSourceType(source: number): [number, number, number][]; |
100 | | -${decl}function __VLS_getVForSourceType(source: string): [string, number, number][]; |
101 | | -${decl}function __VLS_getVForSourceType<T extends any[]>(source: T): [ |
102 | | - item: T[number], |
103 | | - key: number, |
104 | | - index: number, |
105 | | -][]; |
106 | | -${decl}function __VLS_getVForSourceType<T extends { [Symbol.iterator](): Iterator<any> }>(source: T): [ |
107 | | - item: T extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never, |
108 | | - key: number, |
109 | | - index: undefined, |
110 | | -][]; |
111 | | -// #3845 |
112 | | -${decl}function __VLS_getVForSourceType<T extends number | { [Symbol.iterator](): Iterator<any> }>(source: T): [ |
113 | | - item: number | (Exclude<T, number> extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never), |
114 | | - key: number, |
115 | | - index: undefined, |
116 | | -][]; |
117 | | -${decl}function __VLS_getVForSourceType<T>(source: T): [ |
118 | | - item: T[keyof T], |
119 | | - key: keyof T, |
120 | | - index: number, |
121 | | -][]; |
122 | | -// @ts-ignore |
123 | | -${decl}function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>; |
124 | | -// @ts-ignore |
125 | | -${decl}function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0]; |
126 | | -${decl}function __VLS_directiveAsFunction<T extends import('${lib}').Directive>(dir: T): T extends (...args: any) => any |
127 | | - ? T | __VLS_unknownDirective |
128 | | - : NonNullable<(T & Record<string, __VLS_unknownDirective>)['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>; |
129 | | -${decl}function __VLS_withScope<T, K>(ctx: T, scope: K): ctx is T & K; |
130 | | -${decl}function __VLS_makeOptional<T>(t: T): { [K in keyof T]?: T[K] }; |
131 | | -${decl}function __VLS_nonNullable<T>(t: T): T extends null | undefined ? never : T; |
132 | | -${decl}function __VLS_asFunctionalComponent<T, K = T extends new (...args: any) => any ? InstanceType<T> : unknown>(t: T, instance?: K): |
133 | | - T extends new (...args: any) => any |
134 | | - ? (props: ${fnPropsType}, ctx?: any) => __VLS_Element & { __ctx?: { |
135 | | - attrs?: any, |
136 | | - slots?: K extends { ${getSlotsPropertyName(target)}: infer Slots } ? Slots : any, |
137 | | - emit?: K extends { $emit: infer Emit } ? Emit : any |
138 | | - } & { props?: ${fnPropsType}; expose?(exposed: K): void; } } |
139 | | - : T extends () => any ? (props: {}, ctx?: any) => ReturnType<T> |
140 | | - : T extends (...args: any) => any ? T |
141 | | - : (_: {}${strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${strictTemplates ? '' : ' & Record<string, unknown>'} } }; |
142 | | -${decl}function __VLS_elementAsFunction<T>(tag: T, endTag?: T): (_: T${strictTemplates ? '' : ' & Record<string, unknown>'}) => void; |
143 | | -${decl}function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : []; |
144 | | -${decl}function __VLS_pickFunctionalComponentCtx<T, K>(comp: T, compInstance: K): NonNullable<__VLS_PickNotAny< |
145 | | - '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any |
146 | | - , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any |
147 | | ->>; |
148 | | -${decl}function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S; |
149 | | -${decl}function __VLS_tryAsConstant<const T>(t: T): T; |
| 81 | + function __VLS_getVForSourceType(source: number): [number, number, number][]; |
| 82 | + function __VLS_getVForSourceType(source: string): [string, number, number][]; |
| 83 | + function __VLS_getVForSourceType<T extends any[]>(source: T): [ |
| 84 | + item: T[number], |
| 85 | + key: number, |
| 86 | + index: number, |
| 87 | + ][]; |
| 88 | + function __VLS_getVForSourceType<T extends { [Symbol.iterator](): Iterator<any> }>(source: T): [ |
| 89 | + item: T extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never, |
| 90 | + key: number, |
| 91 | + index: undefined, |
| 92 | + ][]; |
| 93 | + // #3845 |
| 94 | + function __VLS_getVForSourceType<T extends number | { [Symbol.iterator](): Iterator<any> }>(source: T): [ |
| 95 | + item: number | (Exclude<T, number> extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never), |
| 96 | + key: number, |
| 97 | + index: undefined, |
| 98 | + ][]; |
| 99 | + function __VLS_getVForSourceType<T>(source: T): [ |
| 100 | + item: T[keyof T], |
| 101 | + key: keyof T, |
| 102 | + index: number, |
| 103 | + ][]; |
| 104 | + // @ts-ignore |
| 105 | + function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>; |
| 106 | + // @ts-ignore |
| 107 | + function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0]; |
| 108 | + function __VLS_directiveAsFunction<T extends import('${lib}').Directive>(dir: T): T extends (...args: any) => any |
| 109 | + ? T | __VLS_unknownDirective |
| 110 | + : NonNullable<(T & Record<string, __VLS_unknownDirective>)['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>; |
| 111 | + function __VLS_withScope<T, K>(ctx: T, scope: K): ctx is T & K; |
| 112 | + function __VLS_makeOptional<T>(t: T): { [K in keyof T]?: T[K] }; |
| 113 | + function __VLS_nonNullable<T>(t: T): T extends null | undefined ? never : T; |
| 114 | + function __VLS_asFunctionalComponent<T, K = T extends new (...args: any) => any ? InstanceType<T> : unknown>(t: T, instance?: K): |
| 115 | + T extends new (...args: any) => any |
| 116 | + ? (props: ${fnPropsType}, ctx?: any) => __VLS_Element & { __ctx?: { |
| 117 | + attrs?: any, |
| 118 | + slots?: K extends { ${getSlotsPropertyName(target)}: infer Slots } ? Slots : any, |
| 119 | + emit?: K extends { $emit: infer Emit } ? Emit : any |
| 120 | + } & { props?: ${fnPropsType}; expose?(exposed: K): void; } } |
| 121 | + : T extends () => any ? (props: {}, ctx?: any) => ReturnType<T> |
| 122 | + : T extends (...args: any) => any ? T |
| 123 | + : (_: {}${strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${strictTemplates ? '' : ' & Record<string, unknown>'} } }; |
| 124 | + function __VLS_elementAsFunction<T>(tag: T, endTag?: T): (_: T${strictTemplates ? '' : ' & Record<string, unknown>'}) => void; |
| 125 | + function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : []; |
| 126 | + function __VLS_pickFunctionalComponentCtx<T, K>(comp: T, compInstance: K): NonNullable<__VLS_PickNotAny< |
| 127 | + '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any |
| 128 | + , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any |
| 129 | + >>; |
| 130 | + function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S; |
| 131 | + function __VLS_tryAsConstant<const T>(t: T): T; |
| 132 | +} |
150 | 133 | `; |
151 | | - |
152 | | - if (mode === 'global') { |
153 | | - str += `}${newLine}`; |
154 | | - } |
155 | | - return str; |
156 | 134 | }; |
0 commit comments