kapsule.d.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. type InitOptions = object;
  2. type PropVal = any;
  3. type StateVal = any;
  4. interface State {
  5. initialised: boolean;
  6. _rerender: () => void;
  7. [stateItem: string]: StateVal;
  8. }
  9. interface PropCfg {
  10. default?: PropVal;
  11. onChange?(newVal: PropVal, state: State, prevVal: PropVal): void;
  12. triggerUpdate?: boolean;
  13. }
  14. type MethodCfg = (state: State, ...args: any[]) => any;
  15. interface KapsuleCfg {
  16. props?: { [prop: string]: PropCfg };
  17. methods?: { [method: string]: MethodCfg };
  18. aliases?: { [propOrMethod: string]: string };
  19. stateInit?: (initOptions?: InitOptions) => Partial<State>;
  20. init?: (
  21. contructorItem?: any,
  22. state?: State,
  23. initOptions?: InitOptions
  24. ) => void;
  25. update: (state?: State, changedProps?: { [prop: string]: PropVal }) => void;
  26. }
  27. type PropGetter = () => PropVal;
  28. type PropSetter = (val: PropVal) => KapsuleInstance;
  29. type KapsuleMethod = (...args: any[]) => any;
  30. interface KapsuleInstance {
  31. (constructorItem: any): KapsuleInstance;
  32. resetProps?(): KapsuleInstance;
  33. [propOrMethod: string]: PropGetter | PropSetter | KapsuleMethod;
  34. }
  35. type KapsuleClosure = (initOptions?: InitOptions) => KapsuleInstance;
  36. declare function Kapsule(cfg?: KapsuleCfg): KapsuleClosure;
  37. export default Kapsule;
  38. export { KapsuleCfg, KapsuleClosure, KapsuleInstance };