Skip to content

Commit

Permalink
feat: defineMixin() allows to write typesafe mixins.
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusBorg committed Nov 5, 2022
1 parent e45e6b0 commit 1fd5e93
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/defineMixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type {
ComputedOptions,
MethodOptions,
ComponentOptionsWithObjectProps,
ComponentOptionsMixin,
ComponentPropsOptions,
EmitsOptions,
} from 'vue'

export /** #__PURE__*/ function defineMixin<
PropsOptions extends Readonly<ComponentPropsOptions>,
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()

0 comments on commit 1fd5e93

Please sign in to comment.