8000 release: vue-class-setup@1.2.9 · dp-os/vue-class-setup@ca7aa7d · GitHub
[go: up one dir, main page]

Skip to content

Commit ca7aa7d

Browse files
committed
release: vue-class-setup@1.2.9
1 parent dfbcd42 commit ca7aa7d

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.9
2+
3+
- feat: `Context` Support props and emit generics
4+
15
## 1.2.8
26

37
- fix: `$vm` return Vue instance on Vue2

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-class-setup",
3-
"version": "1.2.8",
3+
"version": "1.2.9",
44
"main": "dist/index.cjs.js",
55
"module": "dist/index.es.js",
66
"types": "dist/index.d.ts",

src/context.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ function initInject(app: InstanceType<DefineConstructor>, vm: VueInstance) {
125125
});
126126
}
127127

128-
export class Context {
128+
export type DefaultProps = Record<string, any>;
129+
export type DefaultEmit = (...args: any[]) => void;
130+
131+
export class Context<T extends {} = {}, E extends DefaultEmit = DefaultEmit> {
129132
public static [SETUP_NAME] = false;
130133
public static [SETUP_OPTIONS_NAME] = new Map();
131134
public static [SETUP_PROPERTY_DESCRIPTOR] = new Map<
@@ -154,15 +157,15 @@ export class Context {
154157
};
155158
}
156159
public $vm: VueInstance;
157-
public $emit: VueInstance['$emit'];
160+
public $emit: E;
158161
public constructor() {
159162
const vm = getCurrentInstance();
160163
this.$vm = vm ?? ({ $props: {}, $emit: emit } as any);
161-
this.$emit = this.$vm.$emit.bind(this.$vm);
164+
this.$emit = this.$vm.$emit.bind(this.$vm) as E;
162165
setupReference.add(this);
163166
}
164167
public get $props() {
165-
return this.$vm.$props ?? {};
168+
return (this.$vm.$props ?? {}) as T;
166169
}
167170
}
168171
function emit() {}

src/define.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type VueInstance, isVue2 } from './vue';
2-
import { Context } from './context';
2+
import { Context, DefaultProps, DefaultEmit } from './context';
33
import { createDefineProperty } from './property-descriptors';
44
import { SETUP_SETUP_DEFINE } from './config';
55

@@ -20,13 +20,6 @@ interface DefineInstance<T, E> {
2020
readonly $defaultProps: DeepReadonly<Partial<T>>;
2121
}
2222

23-
export type DefaultProps = Record<string, any>;
24-
export type DefaultEmit = (...args: any[]) => void;
25-
26-
// type RequiredBooleanAttr<T extends {}> = {
27-
// [K in keyof T]-?: boolean extends T[K] ? T[K] : T[K];
28-
// };
29-
3023
type DefineInstanceType<
3124
T extends DefaultProps,
3225
E extends DefaultEmit = DefaultEmit

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
export { Setup } from './setup';
22
export { PassOnTo } from './pass-on-to';
3-
export { getCurrentHookContext, Context } from './context';
4-
export { Define, type DefaultEmit, type DefaultProps } from './define';
3+
export {
4+
getCurrentHookContext,
5+
Context,
6+
type DefaultEmit,
7+
type DefaultProps,
8+
} from './context';
9+
export { Define } from './define';
510
export { isVue2, isVue3, getCurrentInstance, type VueInstance } from './vue';
611
export { Watch } from './watch';

src/vue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { getCurrentInstance as get, version } from 'vue';
33
export const isVue2 = /^2\./.test(version);
44
export const isVue3 = /^3\./.test(version);
55

6-
export function getCurrentInstance(): VueInstance {
6+
export function getCurrentInstance(): VueInstance | null {
77
const vm = get();
88
if (vm && vm.proxy) {
99
return vm.proxy as VueInstance;
1010
}
11-
throw new Error(`'getCurrentInstance' use in setup`);
11+
return null;
1212
}
1313

1414
type Instance = NonNullable<NonNullable<ReturnType<typeof get>>['proxy']>;

0 commit comments

Comments
 (0)
0