From 7c425f10cd0d630c7c350fe23cbcc46592b45a96 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Thu, 6 Oct 2022 12:02:12 +0300 Subject: [PATCH] Add DestinationStreamMetadata type to pino.d.ts (#1566) * Add DestinationStreamMetadata type to pino.d.ts Fixes #1557 * Use symbols.needsMetadataGsym instead * Improve DestinationStreamWithMetadata type * Fix tsd weirdness --- pino.d.ts | 12 ++++++++++++ test/types/pino-type-only.test-d.ts | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pino.d.ts b/pino.d.ts index 5442034e1..ffd09439d 100644 --- a/pino.d.ts +++ b/pino.d.ts @@ -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 @@ -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; diff --git a/test/types/pino-type-only.test-d.ts b/test/types/pino-type-only.test-d.ts index 54ea04b19..f106cd28c 100644 --- a/test/types/pino-type-only.test-d.ts +++ b/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`. @@ -14,3 +14,15 @@ expectType(log.info); const level: LevelWithSilent = 'silent'; expectAssignable(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(stream.lastLevel); +}