Skip to content
Open
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
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,26 +279,26 @@ export enum MetricType {

type CollectFunction<T> = (this: T) => void | Promise<void>;

interface MetricObject {
export interface MetricObject {
name: string;
help: string;
type: MetricType;
aggregator: Aggregator;
collect: CollectFunction<any>;
}

interface MetricObjectWithValues<
export interface MetricObjectWithValues<
T extends MetricValue<string>,
> extends MetricObject {
values: T[];
}

type MetricValue<T extends string> = {
export type MetricValue<T extends string> = {
value: number;
labels: LabelValues<T>;
};

type MetricValueWithName<T extends string> = MetricValue<T> & {
export type MetricValueWithName<T extends string> = MetricValue<T> & {
metricName?: string;
};

Expand Down
35 changes: 34 additions & 1 deletion test/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { Counter, Pushgateway, Registry } from '../index';
import {
Counter,
Pushgateway,
Registry,
MetricObject,
MetricObjectWithValues,
MetricValue,
MetricValueWithName,
} from '../index';

const registry = new Registry();
const counter = new Counter({
Expand All @@ -38,3 +46,28 @@ void metricsText;
void gatewayWithRegistry;
void gatewayWithOptionsAndRegistry;
void gatewayWithNullOptionsAndRegistry;

// The metric-object types are exported, so consumers can name the return
// types of Registry#getMetricsAsJSON()/getMetricsAsArray() and Metric#get()
// instead of re-deriving them. These annotations fail to compile if the types
// are removed, renamed, or reshaped.
async function metricObjectTypesAreExported() {
const asJson: MetricObjectWithValues<MetricValue<string>>[] =
await registry.getMetricsAsJSON();
void asJson;

const asArray: MetricObject[] = registry.getMetricsAsArray();
void asArray;

const counterSnapshot: MetricObjectWithValues<MetricValue<string>> =
await counter.get();
void counterSnapshot;

const named: MetricValueWithName<string> = {
value: 1,
labels: {},
metricName: 'typescript_test_counter',
};
void named;
}
void metricObjectTypesAreExported;