Skip to content

Commit

Permalink
fix: stricter type (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 authored Jan 12, 2022
1 parent 65bd615 commit 5e31678
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
10 changes: 6 additions & 4 deletions pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type ThreadStream = any
type TimeFn = () => string;
type MixinFn = () => object;

type CustomLevelLogger<Options> = Options extends LoggerOptions ? Record<keyof Options["customLevels"], LogFn> : never

interface redactOptions {
paths: string[];
censor?: string | ((v: any) => any);
Expand Down Expand Up @@ -207,7 +209,7 @@ declare namespace pino {

type LogDescriptor = Record<string, any>;

type Logger = BaseLogger & LoggerExtras & Record<string, any>;
type Logger<Options = LoggerOptions> = BaseLogger & LoggerExtras & CustomLevelLogger<Options>;

type SerializedError = pinoStdSerializers.SerializedError;
type SerializedResponse = pinoStdSerializers.SerializedResponse;
Expand Down Expand Up @@ -739,15 +741,15 @@ declare namespace pino {
* relative protocol is enabled. Default: process.stdout
* @returns a new logger instance.
*/
declare function pino(optionsOrStream?: LoggerOptions | DestinationStream): Logger;
declare function pino<Options extends LoggerOptions | DestinationStream>(optionsOrStream?: Options): Logger<Options>;

/**
* @param [options]: an options object
* @param [stream]: a writable stream where the logs will be written. It can also receive some log-line metadata, if the
* relative protocol is enabled. Default: process.stdout
* @returns a new logger instance.
*/
declare function pino(options: LoggerOptions, stream: DestinationStream): Logger;
declare function pino<Options extends LoggerOptions>(options: Options, stream: DestinationStream): Logger<Options>;


// Pass through all the top-level exports, allows `import {version} from "pino"`
Expand All @@ -767,7 +769,7 @@ export type Bindings = pino.Bindings;
export type Level = pino.Level;
export type LevelChangeEventListener = pino.LevelChangeEventListener;
export type LogDescriptor = pino.LogDescriptor;
export type Logger = pino.Logger;
export type Logger<Options = LoggerOptions> = pino.Logger<Options>;
export type SerializedError = pino.SerializedError;
export type SerializerFn = pino.SerializerFn;
export type SerializedRequest = pino.SerializedRequest;
Expand Down
11 changes: 8 additions & 3 deletions test/types/pino.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import P, { pino } from "../../";
import { IncomingMessage, ServerResponse } from "http";
import { Socket } from "net";
import { expectError } from 'tsd'
import Logger = P.Logger;

const log = pino();
Expand Down Expand Up @@ -90,6 +91,7 @@ pino({
});

pino({ base: null });
// @ts-expect-error
if ("pino" in log) console.log(`pino version: ${log.pino}`);

log.child({ a: "property" }).info("hello child!");
Expand Down Expand Up @@ -131,9 +133,6 @@ if (log.levelVal === 30) {
console.log("logger level is `info`");
}

log.level = "myLevel";
log.myLevel("a message");

const listener = (lvl: any, val: any, prevLvl: any, prevVal: any) => {
console.log(lvl, val, prevLvl, prevVal);
};
Expand Down Expand Up @@ -304,3 +303,9 @@ const customBaseLogger: CustomBaseLogger = {
silent() {},
child() { return this }
}

// custom levels
const log3 = pino({ customLevels: { myLevel: 100 } })
expectError(log3.log())
log3.level = 'myLevel'
log3.myLevel('')
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"noEmit": true,
"strict": true,
},
"include": [
"exclude": [
"./test/types/*.test-d.ts",
"./*.d.ts"
]
Expand Down

0 comments on commit 5e31678

Please sign in to comment.