All files / src/types options.ts

0% Statements 0/1
100% Branches 0/0
100% Functions 0/0
0% Lines 0/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                                                                                                                                                                                     
import type { Doraemon } from '../instance/init'

export type ExtractComputedReturns<T extends any> = {
  [key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn }
    ? TReturn
    : T[key] extends (...args: any[]) => infer TReturn
      ? TReturn
      : never
}
 
export type ComputedGetter<T> = () => T
export type ComputedSetter<T> = (newValue: T) => void
 
export interface ComputedOptions<T> {
  get: ComputedGetter<T>
  set: ComputedSetter<T>
}
 
export type DefaultData<D> =  object | ((this: D) => object)
export type DefaultProps = Record<string, any>
export type DefaultMethods<D> =  { [key: string]: (this: D, ...args: any[]) => any }
export type DefaultComputed = Record<
  string,
  ComputedGetter<any> | ComputedOptions<any>
>
 
// export type Component<
//   Data=DefaultData<never>,
//   Methods=DefaultMethods<never>,
//   Computed=DefaultComputed,
//   Props=DefaultProps
// > =
//   | typeof Doraemon
//   | ComponentOptions<never, Data, Methods, Computed, Props>
 
export type DefaultComponents<T = {
  ['module']?: string
  type?: 'ancestor' | 'parent' | 'child' | 'descendant'
  observer?: string | Function
}> = {
  [key: string]: string | Partial<T> | (() => (Partial<T>))
}
 
export interface ComponentOptions<
  D extends Doraemon,
  Data = DefaultData<D>,
  Methods = DefaultMethods<D>,
  Computed = DefaultComputed,
  PropsDef=PropsDefinition<DefaultProps>
> {
  data?: Data
  props?: PropsDef
  computed?: Computed
  methods?: Methods
  watch?: Record<string, WatchOptionsWithHandler<any> | WatchHandler<any>>
 
  beforeCreate?(this: D): void
  created?(): void
  mounted?(): void
  /** @deprecated use `unmounted` instead */
  destroyed?(): void
  unmounted?(): void
  errorCaptured?(): void
 
  components?: DefaultComponents
 
  mixins?: (ComponentOptions<Doraemon> | typeof Doraemon)[]
  name?: string
  expose?: string[]
}
 
export type Prop<T> = { (): T } | { new(...args: never[]): T & object } | { new(...args: string[]): Function }
 
export type PropType<T> = Prop<T> | Prop<T>[]
 
export type PropValidator<T> = PropOptions<T> | PropType<T>
 
export interface PropOptions<T = any> {
  type?: PropType<T>
  default?: T | null | undefined | (() => T | null | undefined)
}
 
export type RecordPropsDefinition<T> = {
  [K in keyof T]: PropValidator<T[K]>
}
export type ArrayPropsDefinition<T> = (keyof T)[]
export type PropsDefinition<T> = ArrayPropsDefinition<T> | RecordPropsDefinition<T>
 
export type WatchHandler<T> = string | ((val: T, oldVal: T) => void)
 
export interface WatchOptions {
  deep?: boolean
  immediate?: boolean
}
 
export interface WatchOptionsWithHandler<T> extends WatchOptions {
  handler: WatchHandler<T>
}