Skip to content

Commit d41b1fd

Browse files
authored
🤖 Merge PR DefinitelyTyped#75149 node: swap diagnostics_channel ContextType/StoreType by @Renegade334
1 parent ec361f4 commit d41b1fd

2 files changed

Lines changed: 26 additions & 30 deletions

File tree

‎types/node/diagnostics_channel.d.ts‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ declare module "node:diagnostics_channel" {
3333
* @param name The channel name
3434
* @return The named channel object
3535
*/
36-
function channel(name: string | symbol): Channel;
36+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
37+
function channel<ContextType = any, StoreType = ContextType>(
38+
name: string | symbol,
39+
): Channel<ContextType, StoreType>;
3740
type ChannelListener = (message: unknown, name: string | symbol) => void;
3841
/**
3942
* Register a message handler to subscribe to this channel. This message handler
@@ -96,12 +99,9 @@ declare module "node:diagnostics_channel" {
9699
* @param nameOrChannels Channel name or object containing all the `TracingChannel Channels`
97100
* @return Collection of channels to trace with
98101
*/
99-
function tracingChannel<
100-
StoreType = unknown,
101-
ContextType extends object = StoreType extends object ? StoreType : object,
102-
>(
103-
nameOrChannels: string | TracingChannelCollection<StoreType, ContextType>,
104-
): TracingChannel<StoreType, ContextType>;
102+
function tracingChannel<ContextType extends object = object, StoreType = ContextType>(
103+
nameOrChannels: string | TracingChannelCollection<ContextType, StoreType>,
104+
): TracingChannel<ContextType, StoreType>;
105105
/**
106106
* The class `Channel` represents an individual named channel within the data
107107
* pipeline. It is used to track subscribers and to publish messages when there
@@ -111,7 +111,7 @@ declare module "node:diagnostics_channel" {
111111
* with `new Channel(name)` is not supported.
112112
* @since v15.1.0, v14.17.0
113113
*/
114-
class Channel<StoreType = unknown, ContextType = StoreType> {
114+
class Channel<ContextType = any, StoreType = ContextType> {
115115
readonly name: string | symbol;
116116
/**
117117
* Check if there are active subscribers to this channel. This is helpful if
@@ -304,12 +304,12 @@ declare module "node:diagnostics_channel" {
304304
},
305305
) => void;
306306
}
307-
interface TracingChannelCollection<StoreType = unknown, ContextType = StoreType> {
308-
start: Channel<StoreType, ContextType>;
309-
end: Channel<StoreType, ContextType>;
310-
asyncStart: Channel<StoreType, ContextType>;
311-
asyncEnd: Channel<StoreType, ContextType>;
312-
error: Channel<StoreType, ContextType>;
307+
interface TracingChannelCollection<ContextType extends object = object, StoreType = ContextType> {
308+
start: Channel<ContextType, StoreType>;
309+
end: Channel<ContextType, StoreType>;
310+
asyncStart: Channel<ContextType, StoreType>;
311+
asyncEnd: Channel<ContextType, StoreType>;
312+
error: Channel<ContextType, StoreType>;
313313
}
314314
/**
315315
* The class `TracingChannel` is a collection of `TracingChannel Channels` which
@@ -320,12 +320,9 @@ declare module "node:diagnostics_channel" {
320320
* @since v19.9.0
321321
* @experimental
322322
*/
323-
class TracingChannel<StoreType = unknown, ContextType extends object = {}> implements TracingChannelCollection {
324-
start: Channel<StoreType, ContextType>;
325-
end: Channel<StoreType, ContextType>;
326-
asyncStart: Channel<StoreType, ContextType>;
327-
asyncEnd: Channel<StoreType, ContextType>;
328-
error: Channel<StoreType, ContextType>;
323+
interface TracingChannel<ContextType extends object = object, StoreType = ContextType>
324+
extends TracingChannelCollection<ContextType, StoreType>
325+
{
329326
/**
330327
* Helper to subscribe a collection of functions to the corresponding channels.
331328
* This is the same as calling `channel.subscribe(onMessage)` on each channel

‎types/node/node-tests/diagnostics_channel.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@ unsubscribe(Symbol.for("test-symbol"), listener);
2525
const hasSubs = hasSubscribers("test");
2626

2727
{
28-
const channelsByName = tracingChannel<number, { requestId: number }>("my-channel");
29-
channelsByName.start; // $ExpectType Channel<number, { requestId: number; }>
28+
const channelsByName = tracingChannel<{ requestId: number }, number>("my-channel");
29+
channelsByName.start; // $ExpectType Channel<{ requestId: number }, number>
3030

31-
type MyChannel = Channel<number, { requestId: number }>;
3231
const channelsByCollection = tracingChannel({
33-
start: channel("tracing:my-channel:start") as MyChannel,
34-
end: channel("tracing:my-channel:end") as MyChannel,
35-
asyncStart: channel("tracing:my-channel:asyncStart") as MyChannel,
36-
asyncEnd: channel("tracing:my-channel:asyncEnd") as MyChannel,
37-
error: channel("tracing:my-channel:error") as MyChannel,
32+
start: channel<{ requestId: number }, number>("tracing:my-channel:start"),
33+
end: channel<{ requestId: number }, number>("tracing:my-channel:end"),
34+
asyncStart: channel<{ requestId: number }, number>("tracing:my-channel:asyncStart"),
35+
asyncEnd: channel<{ requestId: number }, number>("tracing:my-channel:asyncEnd"),
36+
error: channel<{ requestId: number }, number>("tracing:my-channel:error"),
3837
});
39-
channelsByCollection.start; // $ExpectType Channel<number, { requestId: number; }>
38+
channelsByCollection.start; // $ExpectType Channel<{ requestId: number }, number>
4039
}
4140

4241
{
43-
const channels = tracingChannel<number, { requestId: number }>("my-channel");
42+
const channels = tracingChannel<{ requestId: number }, number>("my-channel");
4443
const store = new AsyncLocalStorage<number>();
4544

4645
channels.start.bindStore(store);

0 commit comments

Comments
 (0)