Skip to content
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

Add DestinationStreamMetadata type to pino.d.ts #1566

Merged
merged 4 commits into from Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions pino.d.ts
Expand Up @@ -274,6 +274,17 @@ declare namespace pino {
write(msg: string): void;
}

interface DestinationStreamHasMetadata {
[symbols.needsMetadataGsym]: true;
lastLevel: number;
lastTime: string;
lastMsg: string;
lastObj: object;
lastLogger: pino.Logger;
}

type DestinationStreamWithMetadata = DestinationStream & ({ [symbols.needsMetadataGsym]?: false } | DestinationStreamHasMetadata);

interface StreamEntry {
stream: DestinationStream
level?: Level
Expand Down Expand Up @@ -795,6 +806,7 @@ export const version: typeof pino.version;

// Types
export type Bindings = pino.Bindings;
export type DestinationStreamWithMetadata = pino.DestinationStreamWithMetadata;
export type Level = pino.Level;
export type LevelWithSilent = pino.LevelWithSilent;
export type LevelChangeEventListener = pino.LevelChangeEventListener;
Expand Down
14 changes: 13 additions & 1 deletion test/types/pino-type-only.test-d.ts
@@ -1,7 +1,7 @@
import { expectAssignable, expectType } from "tsd";

import pino from "../../";
import type {LevelWithSilent, Logger, LogFn, P} from "../../pino";
import type {LevelWithSilent, Logger, LogFn, P, DestinationStreamWithMetadata } from "../../pino";

// NB: can also use `import * as pino`, but that form is callable as `pino()`
// under `esModuleInterop: false` or `pino.default()` under `esModuleInterop: true`.
Expand All @@ -14,3 +14,15 @@ expectType<P.LogFn>(log.info);

const level: LevelWithSilent = 'silent';
expectAssignable<P.LevelWithSilent>(level);

function createStream(): DestinationStreamWithMetadata {
return { write() {} };
}

const stream = createStream();
// Argh. TypeScript doesn't seem to narrow unless we assign the symbol like so, and tsd seems to
// break without annotating the type explicitly
const needsMetadata: typeof pino.symbols.needsMetadataGsym = pino.symbols.needsMetadataGsym;
if (stream[needsMetadata]) {
expectType<number>(stream.lastLevel);
}