Ver código fonte

feat: allow mixins to accept generics

Evan You 7 anos atrás
pai
commit
62214fa42e
1 arquivos alterados com 8 adições e 3 exclusões
  1. 8 3
      packages/core/src/optional/mixin.ts

+ 8 - 3
packages/core/src/optional/mixin.ts

@@ -4,6 +4,10 @@ interface ComponentConstructor<This = Component> {
   new (): This
 }
 
+interface ComponentConstructorWithMixins<This> {
+  new <P = {}, D = {}>(): This & { $data: D } & D & { $props: Readonly<P> } & P
+}
+
 // mind = blown
 // https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type
 type UnionToIntersection<U> = (U extends any
@@ -17,9 +21,9 @@ type ExtractInstance<T> = T extends (infer U)[]
   : never
 
 export function mixins<
-  T extends ComponentConstructor[],
+  T extends ComponentConstructor[] = [],
   V = ExtractInstance<T>
->(...args: T): ComponentConstructor<V>
+>(...args: T): ComponentConstructorWithMixins<V>
 export function mixins(...args: any[]): any {
   // TODO
 }
@@ -38,10 +42,11 @@ class Bar extends Component<{ bar: string }> {
   }
 }
 
-class Baz extends mixins(Foo, Bar) {
+class Baz extends mixins(Foo, Bar)<{ baz: number }> {
   created() {
     this.foo
     this.bar
+    this.baz
     this.test()
     this.ok()
   }