简体   繁体   English

如何一次更改 object 的多个功能?

[英]How to alter multiple functions of an object at once?

I have an object that looks like this:我有一个 object,看起来像这样:

export declare interface Logger {
  info(msg: string, options?: LogOptions): void;
  warn(msg: string, options?: LogOptions): void;
  warnOnce(msg: string, options?: LogOptions): void;
  error(msg: string, options?: LogOptions): void;
  clearScreen(type: LogType): void;
  hasErrorLogged(error: Error | RollupError): boolean;
  hasWarned: boolean;
}

I want to create a new object that has all the same properties but with altered info , warn , warnOnce and error functions.我想创建一个新的 object,它具有所有相同的属性,但更改了infowarnwarnOnceerror函数。 The changes to each function are the same.每个function的变化都是一样的。

I tried the following but TypeScript says Element implicitly has an 'any' type because expression of type 'string | number | symbol' can't be used to index type我尝试了以下但 TypeScript 说Element implicitly has an 'any' type because expression of type 'string | number | symbol' can't be used to index type Element implicitly has an 'any' type because expression of type 'string | number | symbol' can't be used to index type Element implicitly has an 'any' type because expression of type 'string | number | symbol' can't be used to index type : Element implicitly has an 'any' type because expression of type 'string | number | symbol' can't be used to index type

const alterMsg = (msg: string) => msg.replaceAll('A', 'B');
const alterOptions = {
  flag: false,
};

const logger: Logger = createLogger();
const { clearScreen, hasErrorLogged, hasWarned, ...functionsToAlter } = logger;

Object.keys(functionsToAlter).forEach(key => {
  const originalFunction = functionsToAlter[key as keyof Partial<Logger>];
  const alteredFunction = (msg: string, options?: LogOptions) =>
    originalFunction(alterMsg(msg), {
      ...alterOptions,
      ...options,
    });
  functionsToAlter[key as keyof Partial<Logger>] = alteredFunction;
});

const newLogger: Logger = {
  ...functionsToChange,
  ...logger,
};

The type should stay the same.类型应该保持不变。 It is still the same object structure and each of the functions still accepts a string msg and an optional options object.它仍然是相同的 object 结构,每个函数仍然接受一个字符串msg和一个可选options object。

Kind of partial solution:部分解决方案:

Based on this answer https://stackoverflow.com/a/50593801/5089567 typing destructuration helps infer functionsToChange 's type基于这个答案https://stackoverflow.com/a/50593801/5089567键入解构有助于推断functionsToChange的类型

const { clearScreen, hasErrorLogged, hasWarned, ...functionsToChange }: Logger & Partial<Logger> = logger;

Then instead of然后代替

key as keyof Partial<Logger>

use利用

key as keyof Omit<Logger, 'clearScreen' | 'hasErrorLogged' | 'hasWarned'>

And finally最后

const newLogger: Logger = {
  clearScreen, hasErrorLogged, hasWarned,
  ...functionsToChange,
};

Works fine in playground操场上工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM