-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript error when using PrettyOptions.customPrettifiers.level
with extras
#550
Comments
Thanks for opening a PR! Can you please add a unit test? We use |
Hi @mcollina, I'm happy to contribute a test but I'm not sure what the best way is to fix it. The test change can be as simple as: diff --git a/test/types/pino-pretty.test-d.ts b/test/types/pino-pretty.test-d.ts
index 9f2acd7..2f41344 100644
--- a/test/types/pino-pretty.test-d.ts
+++ b/test/types/pino-pretty.test-d.ts
@@ -32,9 +32,9 @@ const options: PinoPretty.PrettyOptions = {
customPrettifiers: {
key: (value) => {
return value.toString().toUpperCase();
},
- level: (level, label, colorized) => {
+ level: (level, levelKey, log, { label, labelColorized, colors }) => {
return level.toString();
}
},
customLevels: 'verbose:5', And I tried to still use We can though, leverage a We can also simply merge Again I'm new to TypeScript and Node.js so please let me know if you have anything better in mind. I'll be happy to submit PR(s) with your guide. Appendix: diff --git a/index.d.ts b/index.d.ts
index 2c4c5c8..cc44241 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -182,12 +182,9 @@ declare namespace PinoPretty {
* // do some prettify magic
* }
* ```
*/
- customPrettifiers?: Record<string, Prettifier> &
- {
- level?: Prettifier<LevelPrettifierExtras>
- };
+ customPrettifiers?: CustomPrettifiers;
/**
* Change the level names and values to an user custom preset.
*
* Can be a CSV string in 'level_name:level_value' format or an object.
@@ -217,8 +214,9 @@ declare namespace PinoPretty {
}
function build(options: PrettyOptions): PrettyStream;
+ type CustomPrettifiers = Map<'level', Prettifier<LevelPrettifierExtras>> & Map<string, Prettifier>
type Prettifier<T = object> = (inputData: string | object, key: string, log: object, extras: PrettifierExtras<T>) => string;
type PrettifierExtras<T = object> = {colors: Colorette.Colorette} & T;
type LevelPrettifierExtras = {label: string, labelColorized: string}
type MessageFormatFunc = (log: LogDescriptor, messageKey: string, levelLabel: string, extras: PrettifierExtras) => string;
@@ -227,8 +225,8 @@ declare namespace PinoPretty {
type PrettyFactory = typeof prettyFactory;
type Build = typeof build;
type isColorSupported = typeof Colorette.isColorSupported;
- export { build, PinoPretty, PrettyOptions, PrettyStream, colorizerFactory, prettyFactory, isColorSupported };
+ export { build, CustomPrettifiers, PinoPretty, PrettyOptions, PrettyStream, colorizerFactory, prettyFactory, isColorSupported };
}
export = PinoPretty;
diff --git a/test/types/pino-pretty.test-d.ts b/test/types/pino-pretty.test-d.ts
index 9f2acd7..87874c1 100644
--- a/test/types/pino-pretty.test-d.ts
+++ b/test/types/pino-pretty.test-d.ts
@@ -3,8 +3,9 @@ import { expectType } from "tsd";
import pretty from "../../";
import PinoPretty, {
PinoPretty as PinoPrettyNamed,
PrettyOptions,
+ CustomPrettifiers,
colorizerFactory,
prettyFactory
} from "../../";
import PinoPrettyDefault from "../../";
@@ -12,8 +13,15 @@ import * as PinoPrettyStar from "../../";
import PinoPrettyCjsImport = require("../../");
import PrettyStream = PinoPretty.PrettyStream;
const PinoPrettyCjs = require("../../");
+const customPrettifiers: CustomPrettifiers = new Map()
+customPrettifiers.set('key', (value) => {
+ return value.toString().toUpperCase();
+})
+customPrettifiers.set('level', (level, levelKey, log, {label, labelColorized, colors}) => {
+ return level.toString();
+})
const options: PinoPretty.PrettyOptions = {
colorize: true,
crlf: false,
errorLikeObjectKeys: ["err", "error"],
@@ -28,16 +36,9 @@ const options: PinoPretty.PrettyOptions = {
timestampKey: "timestamp",
minimumLevel: "trace",
translateTime: "UTC:h:MM:ss TT Z",
singleLine: false,
- customPrettifiers: {
- key: (value) => {
- return value.toString().toUpperCase();
- },
- level: (level, label, colorized) => {
- return level.toString();
- }
- },
+ customPrettifiers,
customLevels: 'verbose:5',
customColors: 'default:white,verbose:gray',
sync: false,
destination: 2, |
For example,
...leads to
It seems since
Record<string, Prettifier>
doesn't restrict what keys it can have,Prettifier
must be assignable toPrettifier<LevelPrettifierExtras>
?I'm new to TypeScript so maybe it's me that's using it wrong. Please help, thanks!
Edit: I'm using TypeScript v4.8.4, Node.js v18.9.1.
The text was updated successfully, but these errors were encountered: