最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

那意味着什么 ($ winstonLoggerConfig )=> $ winstonLogger 在winston v3?

运维笔记admin10浏览0评论

那意味着什么 ($ winstonLoggerConfig )=> $ winstonLogger 在winston v3?

那意味着什么 ($ winstonLoggerConfig )=> $ winstonLogger 在winston v3?

我正在使用winston记录器并想要输入它。但我不知道应该传递给谁。我的记录器:

const logger = createLogger({
...
});

Missing type annotation for `T`. `T` is a type parameter declared in function type [1] and was implicitly instantiated
at call of `createLogger` [2].

...

   startup/logger.js:35:16
                      v-------------
   35| const logger = createLogger({

References:
   flow-typed/npm/winston_v3.x.x.js:98:19
   98|     createLogger: <T>($winstonLoggerConfig<T>) => $winstonLogger<T>,
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [1]

我也在流式文件库中找到了这个:

declare type $winstonLoggerConfig<T: $winstonLevels> = {
  exitOnError?: boolean,
  format?: $winstonFormat,
  level?: $Keys<T>,
  levels?: T,
  transports?: Array<$winstonTransport>
};

declare type $winstonLogger<T: $winstonLevels> = {
  [$Keys<T>]: (message: string, meta?: Object) => void,
  add: $winstonTransport => void,
  clear: () => void,
  configure: ($winstonLoggerConfig<T>) => void,
  log: (message: $winstonInfo<T>) => void,
  remove: $winstonTransport => void
};

那么我该怎么做呢?

回答如下:

我不知道错误来自哪里,但我可以解释一下T是什么。

这里的T是generic type。当您想要约束类型但不是太多时,这很有用。例如,假设你有一个Bag类型:

type Bag = {
    name: string,
    content: Array<number>
}

您可能会发现限制太多只能将数字放入行李中,假设您希望在某些行李中包含字符串,而在其他行李中包含数字,则可以将类型更改为:

type NumberBag = {
    name: string,
    content: Array<number>
}
type StringBag = {
    name: string,
    content: Array<string>
}

但更好的方法是只对你想要的东西进行约束,这里我们想要的真正约束是“一个袋子只包含一种东西”(stringnumber)。这是泛型类型有用的地方:

type Bag<GenericType> = {
    name: string,
    content: Array<GenericType>
}

现在你可能想要更具体一点,想象你只想要包包含数字或字符串(就像我们之前做的那样):

type Bag<GenericType: number | string> = {
    name: string,
    content: Array<GenericType>
}

好的,现在想象你要申报一个新包:

const firstBag: Bag = {
    name: "Integer bag", 
    content: [1,3,4]
};

如果你只这样做,你会有一个(流)错误说:

Cannot use `Bag` [1] without 1 type argument.
1: type Bag<GenericType: number | string> =

这引用的类型参数是泛型类型(定义了包中的内容)。

换句话说,这意味着,没有“包”有“袋子的东西”,而“东西”需要定义。因此,在创建Bag时,您需要指定要使用的确切Bag类型:

const firstBag: Bag<number> = {
    name: "Integer bag", 
    content: [1,3,4]
};

对于函数和类,它的作用相同,它们都可以通过泛型类型进行参数化。

在你的情况下,你似乎有一个函数createLogger与附加的泛型类型。这种通用类型也被限制为$winstonLevels(这相当于我们对袋子的number | string)。但我认为这里没有指定类型不应该是类型错误,你是否有函数createLogger的声明?

玩具示例的代码是here。


编辑:你顺便使用什么版本的流程?

发布评论

评论列表(0)

  1. 暂无评论