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                                                                                                                                                                           
import { Doraemon } from '../Doraemon'

export interface ComputedOptions<T> {
  get?(): T
  set?(value: T): void
}
 
export type Accessors<T> = {
  [K in keyof T]: (() => T[K]) | ComputedOptions<T[K]>
}
 
type DefaultData<D> =  object | ((this: D) => object)
type DefaultProps = Record<string, any>
type DefaultMethods<D> =  { [key: string]: (this: D, ...args: any[]) => any }
type DefaultComputed = { [key: string]: any }
 
export type Component<
  Data=DefaultData<never>,
  Methods=DefaultMethods<never>,
  Computed=DefaultComputed,
  Props=DefaultProps
> =
  | typeof Doraemon
  | ComponentOptions<never, Data, Methods, Computed, Props>
 
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?: Accessors<Computed>
  methods?: Methods
  watch?: Record<string, WatchOptionsWithHandler<any> | WatchHandler<any>>
 
  beforeCreate?(this: D): void
  created?(): void
  destroyed?(): void
  mounted?(): void
  errorCaptured?(): void
 
  components?: DefaultComponents
 
  mixins?: (ComponentOptions<Doraemon> | typeof Doraemon)[]
  name?: 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>
}