Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

mixins with parameters typescript

// Utility types:

type GetProps<TBase> = TBase extends new (props: infer P) => any ? P : never
type GetInstance<TBase> = TBase extends new (...args: any[]) => infer I ? I : never
type MergeCtor<A, B> = new (props: GetProps<A> & GetProps<B>) => GetInstance<A> & GetInstance<B>


// Usage:
// bypass the restriction and manually type the signature
function GeometryMixin<TBase extends MixinBase>(Base: TBase) {
  // key 1: assert Base as any to mute the TS error
  const Derived = class Geometry extends (Base as any) {
    shape: 'rectangle' | 'triangle'
    constructor(props: { shape: 'rectangle' | 'triangle' }) {
      super(props)
      this.shape = props.shape
    }
  }

  // key 2: manually cast type to be MergeCtor
  return Derived as MergeCtor<typeof Derived, TBase>
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: benefits of ginger juice 
Typescript :: how to get all dates from range in react js 
Typescript :: check if all elements in array can be divided by python 
Typescript :: mat-autocomplete options dropdown does not stick when scrolling 
Typescript :: how to call a function in a macro with variadic arguments c++ 
Typescript :: get Nested Iteration index Count in Angular 13 
Typescript :: About half of divorced parents try to avoid each other after the divorce, creating a different set of rules for children to follow in each parent’s household. This type of parental interaction is called 
Typescript :: nestjs called every X second method 
Typescript :: how to append different lists in installed apps django 
Typescript :: nest js env validation 
Typescript :: ips in range typescript 
Typescript :: typescript reset class properties to default 
Typescript :: ts Template pattern 
Typescript :: modify objects using dot notation 
Typescript :: PYTHON STACK FUNCTION count the valid number of brackets Returns the total number of valid brackets in the string 
Typescript :: cuisine types list in array 
Typescript :: fiber absorption loss measurement 
Typescript :: set typescript 
Typescript :: whats a company letterhead 
Typescript :: Give a brief description of the process of synthesis of food in green plants. 
Typescript :: // running tests Your code should no longer have a p tag around the text asking what level ninja a user is. // tests completed category:423 
Cpp :: c++ show time elapsed 
Cpp :: print 2d vector c++ 
Cpp :: iterator on std::tuple 
Cpp :: calculate time difference cpp 
Cpp :: how to find index of a vector in c++ 
Cpp :: how to append one vector to another c++ 
Cpp :: constant pointer c++ 
Cpp :: insert at position in vector c++ 
Cpp :: perulangan c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =