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