-
Notifications
You must be signed in to change notification settings - Fork 152
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, |
@Frederick888 that works! Would you like to send a PR? |
The fourth parameter of the 'level' custom prettifier -- extras -- used to have a separate type as pino-pretty offered two additional attributes to this particular prettifier. However since it used a Record<string, T> type, TypeScript required all values to be compatible with each other. So when users tried to use the extras parameter for 'level', it caused error TS2322: Type '{ level: (_level: string | object, _levelKey: string, _log: object, { labelColorized }: PrettifierExtras<LevelPrettifierExtras>) => string; }' is not assignable to type 'Record<string, Prettifier<object>> & { level?: Prettifier<LevelPrettifierExtras> | undefined; }'. Type '{ level: (_level: string | object, _levelKey: string, _log: object, { labelColorized }: PrettifierExtras<LevelPrettifierExtras>) => string; }' is not assignable to type 'Record<string, Prettifier<object>>'. Property 'level' is incompatible with index signature. Type '(_level: string | object, _levelKey: string, _log: object, { labelColorized }: PrettifierExtras<LevelPrettifierExtras>) => string' is not assignable to type 'Prettifier<object>'. Types of parameters '__3' and 'extras' are incompatible. Type 'PrettifierExtras<object>' is not assignable to type 'PrettifierExtras<LevelPrettifierExtras>'. Type '{ colors: Colorette; }' is missing the following properties from type 'LevelPrettifierExtras': label, labelColorized 115 customPrettifiers: { ~~~~~~~~~~~~~~~~~ This patch remedies this issue by merging LevelPrettifierExtras into PrettifierExtras. These types are not exported directly, therefore users, including those who leverage TypeScript utility types to extract these types, should be able to upgrade directly. Fixes pinojs#550
In [1], LevelPrettifierExtras and PrettifierExtras were merged to solve a typing issue [2]. This was suboptimal as the additional extra attributes that only the 'level' prettifier had were exposed to other prettifiers too. To allow the additional attributes while keeping 'level' prettifier's type separate, this patch uses an intersection Map type CustomPrettifiers instead. This is a breaking change. [1] pinojs#551 [2] pinojs#550
In [1], LevelPrettifierExtras and PrettifierExtras were merged to solve a typing issue [2]. This was suboptimal as the additional extra attributes that only the 'level' prettifier had were exposed to other prettifiers too. To allow the additional attributes while keeping 'level' prettifier's type separate, this patch uses an intersection Map type CustomPrettifiers instead. This is a breaking change. [1] pinojs#551 [2] pinojs#550
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: