Skip to content

从字段到函数的推导

Code

ts
const personWatcher = watch({
  firstName: 'Saoirse',
  lastName: 'Ronan',
  age: 26,
  sex: '男',
});
personWatch.on('firstNameChanged', (oldValue, newValue) => {});

解析

ts
type Watcher<T> = {
  on<K extends keyof T & string>(
    eventName: `${K}Changed`,
    callback: (oldValue: T[K], newValue: T[K]) => void
  ): void;
};
declare function watch<T>(obj: T): Watcher<T>;