tg-map-vue3 3.0.8 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- import type { Component, ComponentOptions, ComponentPublicInstance, Prop, PropType, RendererElement, RendererNode, Slots, VNode, WritableComputedOptions } from 'vue';
2
- import type { AbstractConstructor, KeysMatching, StringEnumLike, Thing } from './mapped-types';
1
+ import { type Component, type ComponentOptions, type ComponentPublicInstance, type Prop, type PropType, type RendererElement, type RendererNode, type Slots, type VNode, type WritableComputedOptions } from 'vue';
2
+ import type { AbstractConstructor, KeysMatching, StringEnumLike, StringEnumValue, Thing } from './mapped-types';
3
3
  /** 组件选项中的hook名 */
4
4
  export type VueHookName = ExcludeSubtype<KeysMatching<OmitStartsWith<RemoveIndex<ComponentOptions>, '_'>, {
5
5
  (): void;
@@ -39,18 +39,18 @@ type NotNullProp<T = any, D = T> = {
39
39
  default: D | (() => D);
40
40
  });
41
41
  export declare function stringEnumProp<E extends StringEnumLike>(enumObject: E, defaultValue: E[keyof E]): {
42
- type: PropType<E[keyof E]>;
42
+ type: PropType<StringEnumValue<E>>;
43
43
  default: E[keyof E];
44
44
  };
45
45
  export declare function stringEnumProp<E extends StringEnumLike>(enumObject: E): {
46
- type: PropType<E[keyof E]>;
46
+ type: PropType<StringEnumValue<E>>;
47
47
  };
48
- export declare function stringEnumPropFromArray<T extends string>(enums: T[], defaultValue: T): {
49
- type: PropType<T>;
48
+ export declare function stringUnionPropFromValues<T extends string>(values: readonly T[], defaultValue: T): {
49
+ type: PropType<T[][number]>;
50
50
  default: T;
51
51
  };
52
- export declare function stringEnumPropFromArray<T extends string>(enums: T[]): {
53
- type: PropType<T>;
52
+ export declare function stringUnionPropFromValues<T extends string>(values: readonly T[]): {
53
+ type: PropType<T[][number]>;
54
54
  };
55
55
  export declare function requiredProp<T>(type: PropType<T>): {
56
56
  type: PropType<T>;
@@ -110,20 +110,69 @@ export declare function findByComponentType<T extends Component & AbstractConstr
110
110
  */
111
111
  export declare function extractVNodeFromSlotsByComponent<Props>(slots: Slots, component: ComponentOptions<Props>): VNode<RendererNode, RendererElement, Props> | undefined;
112
112
  /**
113
- * Vue3中 class/style/未声明的事件/未声明的属性 都会集合到{@link SetupContext.attrs}中
114
- * 默认情况下({@link ComponentOptions.inheritAttrs}为true), 将被透传给组件的根节点
113
+ * Vue3中 class/style/未声明的事件/未声明的属性 都会集合到`this.$attrs`中
114
+ * 默认情况下(`inheritAttrs`为true), 将被透传给组件的根节点
115
+ *
116
+ * 而Vue2, `this.$attrs`值包含 未声明的属性, 所有事件放到了`this.$listeners`中
115
117
  *
116
- * 而Vue2, 默认只会透传 未声明的属性, 事件通过`$listeners`暴露
118
+ * 这里模仿Vue2的行为, 从{@link attrs}中拆分出未声明的事件, 方便将他们透传给地图的对象
119
+ * 同时若传入{@link props}, 也会从它里面拆分出 声明的事件类型的属性
117
120
  *
118
- * 这里模仿Vue2的行为, 拆分出未声明的事件, 方便将他们透传给地图的对象
121
+ * ## 如何给事件添加类型信息
119
122
  *
120
- * @see https://cn.vuejs.org/guide/components/attrs.html#disabling-attribute-inheritance
121
- * @see https://v3-migration.vuejs.org/zh/breaking-changes/listeners-removed.html
123
+ * - 声明在`emits`中的事件, 会给emit方法添加类型信息, 但没办法在运行时判断是否有被设置
124
+ * - 声明在`props`中的事件, 不会.................. , 但可以通过读取`this.$props`判断是否被设置
125
+ *
126
+ * 所有我们最终选择用{@link EventProps}在`props`中声明事件, 同时用{@link EventEmits}在`emits`上声明事件的类型,
127
+ * 达到既有类型信息, 又能判断是否存在的效果, 写法如下:
128
+ * ```
129
+ * {
130
+ * props: {...} satisfies EventProps<TgMapEmits>,
131
+ * emits: undefined as any as EventEmits<TgMapEmits>,
132
+ * }
133
+ * ```
134
+ *
135
+ * ## 参考
136
+ * - https://cn.vuejs.org/guide/components/attrs.html#disabling-attribute-inheritance
137
+ * - https://v3-migration.vuejs.org/zh/breaking-changes/listeners-removed.html
138
+ * - https://eslint.vuejs.org/rules/require-explicit-emits.html#options
122
139
  */
123
- export declare function splitAttrs(attrs: Record<string, unknown>): {
140
+ export declare function splitAttrs(attrs: Record<string, unknown>, props?: Record<string, unknown>): {
141
+ /** class/style/未声明的属性 等 */
142
+ binds: Record<string, unknown>;
143
+ /** 未声明的事件 */
144
+ listeners: Record<string, unknown>;
145
+ /** 声明在`props`中的事件 */
146
+ listenerProps: Record<string, unknown>;
147
+ };
148
+ /** 响应式的{@link splitAttrs} */
149
+ export declare function useSplittedAttrs(attrs: Record<string, unknown>, props?: Record<string, unknown>): import("vue").ComputedRef<{
150
+ /** class/style/未声明的属性 等 */
124
151
  binds: Record<string, unknown>;
152
+ /** 未声明的事件 */
125
153
  listeners: Record<string, unknown>;
154
+ /** 声明在`props`中的事件 */
155
+ listenerProps: Record<string, unknown>;
156
+ }>;
157
+ export type EventCallback<E = any> = (event: E) => void;
158
+ /**
159
+ * 事件类型的属性
160
+ * @see splitAttrs
161
+ * */
162
+ export type EventProps<Emits> = Emits extends string ? {
163
+ [K in Emits as `on${Capitalize<K>}`]: Prop<Function>;
164
+ } : {
165
+ [K in keyof Emits as K extends string ? `on${Capitalize<K>}` : never]: Prop<EventCallback<Emits[K]>>;
166
+ };
167
+ /**
168
+ * 用来声明事件的类型信息
169
+ * @see splitAttrs
170
+ * */
171
+ export type EventEmits<T> = {
172
+ [K in keyof T]: (event: T[K]) => boolean;
126
173
  };
174
+ /** 给事件类型的属性标注类型 */
175
+ export declare function eventProp<E = any>(): Prop<EventCallback<E>>;
127
176
  export declare const eventLogMethods: {
128
177
  /**
129
178
  * @event 一般来说传{@link Tg.Event}对象, 但传其他对象也不会报错