Skip to content

Commit

Permalink
fix: type of child logger with custom levels (#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
UndefinedBehaviour committed Dec 26, 2023
1 parent 05b70e4 commit 44d06c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ declare namespace pino {
/**
* Optional child creation callback.
*/
onChild?: OnChildCallback;
onChild?: OnChildCallback<CustomLevels>;

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


// Pass through all the top-level exports, allows `import {version} from "pino"`
Expand Down
20 changes: 20 additions & 0 deletions test/types/pino.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,23 @@ expectType<Logger<'log'>>(pino({
},
},
}))

const parentLogger1 = pino({
customLevels: { myLevel: 90 },
onChild: (child) => { const a = child.myLevel; }
}, process.stdout)
parentLogger1.onChild = (child) => { child.myLevel(''); }

const childLogger1 = parentLogger1.child({});
childLogger1.myLevel('');
expectError(childLogger1.doesntExist(''));

const parentLogger2 = pino({}, process.stdin);
expectError(parentLogger2.onChild = (child) => { const b = child.doesntExist; });

const childLogger2 = parentLogger2.child({});
expectError(childLogger2.doesntExist);

expectError(pino({
onChild: (child) => { const a = child.doesntExist; }
}, process.stdout));

0 comments on commit 44d06c8

Please sign in to comment.