Skip to content

Commit

Permalink
Add DestinationStreamMetadata type to pino.d.ts (#1566)
Browse files Browse the repository at this point in the history
* Add DestinationStreamMetadata type to pino.d.ts

Fixes #1557

* Use symbols.needsMetadataGsym instead

* Improve DestinationStreamWithMetadata type

* Fix tsd weirdness
  • Loading branch information
segevfiner committed Oct 6, 2022
1 parent f31243a commit 7c425f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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);
}

0 comments on commit 7c425f1

Please sign in to comment.