Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions types/node/diagnostics_channel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ declare module "node:diagnostics_channel" {
* @param name The channel name
* @return The named channel object
*/
function channel(name: string | symbol): Channel;
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
function channel<ContextType = any, StoreType = ContextType>(
name: string | symbol,
): Channel<ContextType, StoreType>;
type ChannelListener = (message: unknown, name: string | symbol) => void;
/**
* Register a message handler to subscribe to this channel. This message handler
Expand Down Expand Up @@ -96,12 +99,9 @@ declare module "node:diagnostics_channel" {
* @param nameOrChannels Channel name or object containing all the `TracingChannel Channels`
* @return Collection of channels to trace with
*/
function tracingChannel<
StoreType = unknown,
ContextType extends object = StoreType extends object ? StoreType : object,
>(
nameOrChannels: string | TracingChannelCollection<StoreType, ContextType>,
): TracingChannel<StoreType, ContextType>;
function tracingChannel<ContextType extends object = object, StoreType = ContextType>(
nameOrChannels: string | TracingChannelCollection<ContextType, StoreType>,
): TracingChannel<ContextType, StoreType>;
/**
* The class `Channel` represents an individual named channel within the data
* pipeline. It is used to track subscribers and to publish messages when there
Expand All @@ -111,7 +111,7 @@ declare module "node:diagnostics_channel" {
* with `new Channel(name)` is not supported.
* @since v15.1.0, v14.17.0
*/
class Channel<StoreType = unknown, ContextType = StoreType> {
class Channel<ContextType = any, StoreType = ContextType> {
readonly name: string | symbol;
/**
* Check if there are active subscribers to this channel. This is helpful if
Expand Down Expand Up @@ -304,12 +304,12 @@ declare module "node:diagnostics_channel" {
},
) => void;
}
interface TracingChannelCollection<StoreType = unknown, ContextType = StoreType> {
start: Channel<StoreType, ContextType>;
end: Channel<StoreType, ContextType>;
asyncStart: Channel<StoreType, ContextType>;
asyncEnd: Channel<StoreType, ContextType>;
error: Channel<StoreType, ContextType>;
interface TracingChannelCollection<ContextType extends object = object, StoreType = ContextType> {
start: Channel<ContextType, StoreType>;
end: Channel<ContextType, StoreType>;
asyncStart: Channel<ContextType, StoreType>;
asyncEnd: Channel<ContextType, StoreType>;
error: Channel<ContextType, StoreType>;
}
/**
* The class `TracingChannel` is a collection of `TracingChannel Channels` which
Expand All @@ -320,12 +320,9 @@ declare module "node:diagnostics_channel" {
* @since v19.9.0
* @experimental
*/
class TracingChannel<StoreType = unknown, ContextType extends object = {}> implements TracingChannelCollection {
start: Channel<StoreType, ContextType>;
end: Channel<StoreType, ContextType>;
asyncStart: Channel<StoreType, ContextType>;
asyncEnd: Channel<StoreType, ContextType>;
error: Channel<StoreType, ContextType>;
interface TracingChannel<ContextType extends object = object, StoreType = ContextType>
extends TracingChannelCollection<ContextType, StoreType>
{
/**
* Helper to subscribe a collection of functions to the corresponding channels.
* This is the same as calling `channel.subscribe(onMessage)` on each channel
Expand Down
19 changes: 9 additions & 10 deletions types/node/node-tests/diagnostics_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,21 @@ unsubscribe(Symbol.for("test-symbol"), listener);
const hasSubs = hasSubscribers("test");

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

type MyChannel = Channel<number, { requestId: number }>;
const channelsByCollection = tracingChannel({
start: channel("tracing:my-channel:start") as MyChannel,
end: channel("tracing:my-channel:end") as MyChannel,
asyncStart: channel("tracing:my-channel:asyncStart") as MyChannel,
asyncEnd: channel("tracing:my-channel:asyncEnd") as MyChannel,
error: channel("tracing:my-channel:error") as MyChannel,
start: channel<{ requestId: number }, number>("tracing:my-channel:start"),
end: channel<{ requestId: number }, number>("tracing:my-channel:end"),
asyncStart: channel<{ requestId: number }, number>("tracing:my-channel:asyncStart"),
asyncEnd: channel<{ requestId: number }, number>("tracing:my-channel:asyncEnd"),
error: channel<{ requestId: number }, number>("tracing:my-channel:error"),
});
channelsByCollection.start; // $ExpectType Channel<number, { requestId: number; }>
channelsByCollection.start; // $ExpectType Channel<{ requestId: number }, number>
}

{
const channels = tracingChannel<number, { requestId: number }>("my-channel");
const channels = tracingChannel<{ requestId: number }, number>("my-channel");
const store = new AsyncLocalStorage<number>();

channels.start.bindStore(store);
Expand Down
5 changes: 5 additions & 0 deletions types/object-treeify/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!**/*.d.ts
!**/*.d.cts
!**/*.d.mts
!**/*.d.*.ts
23 changes: 23 additions & 0 deletions types/object-treeify/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export type RenderFunction = (node: unknown) => unknown;

export type SortFunction = (firstKey: string, secondKey: string) => number;

export interface Options {
joined?: boolean;
spacerNoNeighbour?: string;
spacerNeighbour?: string;
keyNoNeighbour?: string;
keyNeighbour?: string;
separator?: string;
renderFn?: RenderFunction;
sortFn?: SortFunction | null;
breakCircularWith?: string | null;
}

declare function treeify(tree: object, options: Options & { joined: false }): string[];

declare function treeify(tree: object, options?: Options & { joined?: true }): string;

declare function treeify(tree: object, options: Options): string | string[];

export default treeify;
30 changes: 30 additions & 0 deletions types/object-treeify/object-treeify-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import treeify, { type Options, type RenderFunction, type SortFunction } from "object-treeify";

const tree = {
parent: {
child: "value",
},
};

// $ExpectType string
treeify(tree);

// $ExpectType string[]
treeify(tree, { joined: false });

const renderFn: RenderFunction = (node) => node;
const sortFn: SortFunction = (firstKey, secondKey) => firstKey.localeCompare(secondKey);
const options: Options = {
renderFn,
sortFn,
breakCircularWith: null,
};

// $ExpectType string | string[]
treeify(tree, options);

// @ts-expect-error for unsupported option
treeify(tree, { unknownOption: true });

// @ts-expect-error for invalid return type
treeify(tree, { sortFn: () => "invalid" });
18 changes: 18 additions & 0 deletions types/object-treeify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"private": true,
"name": "@types/object-treeify",
"version": "5.0.9999",
"type": "module",
"projects": [
"https://github.com/blackflux/object-treeify"
],
"devDependencies": {
"@types/object-treeify": "workspace:."
},
"owners": [
{
"name": "Patrik Csak",
"githubUsername": "patrik-csak"
}
]
}
19 changes: 19 additions & 0 deletions types/object-treeify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "node16",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"object-treeify-tests.ts"
]
}