From 1fd5e930f28745218519c01d34215b511458d3a7 Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Sat, 5 Nov 2022 10:17:00 +0100 Subject: [PATCH] feat: defineMixin() allows to write typesafe mixins. --- src/defineMixin.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/defineMixin.ts diff --git a/src/defineMixin.ts b/src/defineMixin.ts new file mode 100644 index 0000000..fc1f250 --- /dev/null +++ b/src/defineMixin.ts @@ -0,0 +1,68 @@ +import type { + ComputedOptions, + MethodOptions, + ComponentOptionsWithObjectProps, + ComponentOptionsMixin, + ComponentPropsOptions, + EmitsOptions, +} from 'vue' + +export /** #__PURE__*/ function defineMixin< + PropsOptions extends Readonly, + RawBindings, + D, + C extends ComputedOptions = {}, + M extends MethodOptions = {}, + Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, + Extends extends ComponentOptionsMixin = ComponentOptionsMixin, + E extends EmitsOptions = {}, + EE extends string = string +>( + options: ComponentOptionsWithObjectProps< + PropsOptions, + RawBindings, + D, + C, + M, + Mixin, + Extends, + E, + EE + > +): typeof options { + return options +} + +// Test + +const composable = defineMixin({ + props: {}, + data() { + return { + count: 0, + } + }, + watch: { + count(count) { + console.log('count', count, this.count) + }, + }, + created() { + this.count + }, + computed: { + doubleCount(): number { + return this.count * 2 + }, + }, + methods: { + logCounts() { + console.log(this.count, this.doubleCount) + return true + }, + }, +}) + +composable.count.value +composable.doubleCount.value +composable.logCounts()