Skip to content

Commit 44d06c8

Browse files
fix: type of child logger with custom levels (#1871)
1 parent 05b70e4 commit 44d06c8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pino.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ declare namespace pino {
630630
/**
631631
* Optional child creation callback.
632632
*/
633-
onChild?: OnChildCallback;
633+
onChild?: OnChildCallback<CustomLevels>;
634634

635635
/**
636636
* logs newline delimited JSON with `\r\n` instead of `\n`. Default: `false`.
@@ -799,7 +799,7 @@ declare function pino<CustomLevels extends string = never>(optionsOrStream?: Log
799799
* relative protocol is enabled. Default: process.stdout
800800
* @returns a new logger instance.
801801
*/
802-
declare function pino<CustomLevels extends string>(options: LoggerOptions<CustomLevels>, stream: DestinationStream): Logger<CustomLevels>;
802+
declare function pino<CustomLevels extends string = never>(options: LoggerOptions<CustomLevels>, stream: DestinationStream): Logger<CustomLevels>;
803803

804804

805805
// Pass through all the top-level exports, allows `import {version} from "pino"`

test/types/pino.test-d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,23 @@ expectType<Logger<'log'>>(pino({
404404
},
405405
},
406406
}))
407+
408+
const parentLogger1 = pino({
409+
customLevels: { myLevel: 90 },
410+
onChild: (child) => { const a = child.myLevel; }
411+
}, process.stdout)
412+
parentLogger1.onChild = (child) => { child.myLevel(''); }
413+
414+
const childLogger1 = parentLogger1.child({});
415+
childLogger1.myLevel('');
416+
expectError(childLogger1.doesntExist(''));
417+
418+
const parentLogger2 = pino({}, process.stdin);
419+
expectError(parentLogger2.onChild = (child) => { const b = child.doesntExist; });
420+
421+
const childLogger2 = parentLogger2.child({});
422+
expectError(childLogger2.doesntExist);
423+
424+
expectError(pino({
425+
onChild: (child) => { const a = child.doesntExist; }
426+
}, process.stdout));

0 commit comments

Comments
 (0)