gpt4 book ai didi

javascript - 在 winston v3 中 T there ($winston Logger Config) => $winston Logger 是什么意思?

转载 作者:行者123 更新时间:2023-11-30 08:20:21 25 4
gpt4 key购买 nike

我正在使用 winston logger 和 wnat 进行流式输入。但我不知道我应该传递给什么。我的记录器:

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>
}

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

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> =

this 所指的类型参数是泛型类型(定义包中的内容)。

换句话说,这意味着没有“Bags”,只有“Bags of things”,需要定义“thing”。因此,在创建 Bag 时,您需要指定要使用的确切 Bag 类型:

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

这对函数和类来说是一样的,它们都可以被泛型参数化。

在你的情况下,你似乎有一个函数 createLogger 附加了一个通用类型。此泛型类型也被限制为 $winstonLevels(这相当于我们用于包的 number | string)。但是我觉得这里不指定类型应该不会是类型错误,你有createLogger函数的声明吗?

玩具示例的代码是 here .


编辑:顺便问一下,您使用的是什么版本的流?

关于javascript - 在 winston v3 中 T there <A>($winston Logger Config<T>) => $winston Logger<T> 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54619401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com