From 1c68dd18ab7fb61fcca32d99f1a529edd29b2aca Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Mon, 26 Sep 2022 18:59:56 +0300 Subject: [PATCH 1/4] Add DestinationStreamMetadata type to pino.d.ts Fixes #1557 --- pino.d.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pino.d.ts b/pino.d.ts index 5442034e1..6d3751022 100644 --- a/pino.d.ts +++ b/pino.d.ts @@ -273,6 +273,19 @@ declare namespace pino { interface DestinationStream { write(msg: string): void; } + + const metadata = Symbol.for("pino.metadata"); + + interface DestinationStreamHasMetadata { + [metadata]: true; + lastLevel: number; + lastTime: string; + lastMsg: string; + lastObj: object; + lastLogger: pino.Logger; + } + + type DestinationStreamMetadata = { [metadata]?: boolean } | DestinationStreamHasMetadata; interface StreamEntry { stream: DestinationStream @@ -795,6 +808,7 @@ export const version: typeof pino.version; // Types export type Bindings = pino.Bindings; +export type DestinationStreamMetadata = pino.DestinationStreamMetadata; export type Level = pino.Level; export type LevelWithSilent = pino.LevelWithSilent; export type LevelChangeEventListener = pino.LevelChangeEventListener; From 11bae48f751b60dde5afd1f54d872cd8e36d9089 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Mon, 3 Oct 2022 03:02:52 +0300 Subject: [PATCH 2/4] Use symbols.needsMetadataGsym instead --- pino.d.ts | 8 +++----- test/types/pino-type-only.test-d.ts | 13 ++++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pino.d.ts b/pino.d.ts index 6d3751022..9c122a045 100644 --- a/pino.d.ts +++ b/pino.d.ts @@ -273,11 +273,9 @@ declare namespace pino { interface DestinationStream { write(msg: string): void; } - - const metadata = Symbol.for("pino.metadata"); interface DestinationStreamHasMetadata { - [metadata]: true; + [symbols.needsMetadataGsym]: true; lastLevel: number; lastTime: string; lastMsg: string; @@ -285,7 +283,7 @@ declare namespace pino { lastLogger: pino.Logger; } - type DestinationStreamMetadata = { [metadata]?: boolean } | DestinationStreamHasMetadata; + type DestinationStreamWithMetadata = (DestinationStream & { [symbols.needsMetadataGsym]?: false }) | DestinationStreamHasMetadata; interface StreamEntry { stream: DestinationStream @@ -808,7 +806,7 @@ export const version: typeof pino.version; // Types export type Bindings = pino.Bindings; -export type DestinationStreamMetadata = pino.DestinationStreamMetadata; +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..9388f776b 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,14 @@ 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 +const needsMetadata = pino.symbols.needsMetadataGsym; +if (stream[needsMetadata]) { + expectType(stream.lastLevel); +} From 6ac80852f4d3e4a9726016048e43b0d3b24d7bd1 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 4 Oct 2022 02:27:22 +0300 Subject: [PATCH 3/4] Improve DestinationStreamWithMetadata type --- pino.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pino.d.ts b/pino.d.ts index 9c122a045..ffd09439d 100644 --- a/pino.d.ts +++ b/pino.d.ts @@ -283,7 +283,7 @@ declare namespace pino { lastLogger: pino.Logger; } - type DestinationStreamWithMetadata = (DestinationStream & { [symbols.needsMetadataGsym]?: false }) | DestinationStreamHasMetadata; + type DestinationStreamWithMetadata = DestinationStream & ({ [symbols.needsMetadataGsym]?: false } | DestinationStreamHasMetadata); interface StreamEntry { stream: DestinationStream From 6f0cba5c4eb00672b95f67631426dda24b85c526 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 4 Oct 2022 22:23:52 +0300 Subject: [PATCH 4/4] Fix tsd weirdness --- test/types/pino-type-only.test-d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/types/pino-type-only.test-d.ts b/test/types/pino-type-only.test-d.ts index 9388f776b..f106cd28c 100644 --- a/test/types/pino-type-only.test-d.ts +++ b/test/types/pino-type-only.test-d.ts @@ -20,8 +20,9 @@ function createStream(): DestinationStreamWithMetadata { } const stream = createStream(); -// Argh. TypeScript doesn't seem to narrow unless we assign the symbol like so -const needsMetadata = pino.symbols.needsMetadataGsym; +// 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); }