Skip to content

Commit 4f6857a

Browse files
authored
fix(log): better logfn handling of generics (#2309)
Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
1 parent 833e5da commit 4f6857a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

pino.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ declare namespace pino {
341341
: Acc;
342342

343343
export interface LogFn {
344+
// Simple case: When first argument is always a string message, use parsed arguments directly
344345
<TMsg extends string = string>(msg: TMsg, ...args: ParseLogFnArgs<TMsg>): void;
346+
// Complex case: When first argument can be any type - if it's a string, no message needed; otherwise require a message
347+
<T, TMsg extends string = string>(obj: T, msg?: T extends string ? never: TMsg, ...args: ParseLogFnArgs<TMsg> | []): void;
348+
// Complex case with type safety: Same as above but ensures ParseLogFnArgs is a valid tuple before using it
345349
<T, TMsg extends string = string>(obj: T, msg?: T extends string ? never : TMsg, ...args: ParseLogFnArgs<TMsg> extends [unknown, ...unknown[]] ? ParseLogFnArgs<TMsg> : unknown[]): void;
346350
}
347351

test/types/pino-type-only.test-d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ const loggerOptions:LoggerOptions = {
5757
}
5858

5959
expectType<LoggerOptions>(loggerOptions)
60+
61+
// Reference: https://github.com/pinojs/pino/issues/2285
62+
const someConst = "test" as const;
63+
pino().error({}, someConst);
64+
const someFunc = <T extends typeof someConst>(someConst: T) => {
65+
pino().error({}, someConst);
66+
};

0 commit comments

Comments
 (0)