diff --git a/index.d.ts b/index.d.ts index 8c0ebb2c..8c45dae3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -279,7 +279,7 @@ export enum MetricType { type CollectFunction = (this: T) => void | Promise; -interface MetricObject { +export interface MetricObject { name: string; help: string; type: MetricType; @@ -287,18 +287,18 @@ interface MetricObject { collect: CollectFunction; } -interface MetricObjectWithValues< +export interface MetricObjectWithValues< T extends MetricValue, > extends MetricObject { values: T[]; } -type MetricValue = { +export type MetricValue = { value: number; labels: LabelValues; }; -type MetricValueWithName = MetricValue & { +export type MetricValueWithName = MetricValue & { metricName?: string; }; diff --git a/test/typescript.ts b/test/typescript.ts index 80d899ce..36900684 100644 --- a/test/typescript.ts +++ b/test/typescript.ts @@ -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({ @@ -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>[] = + await registry.getMetricsAsJSON(); + void asJson; + + const asArray: MetricObject[] = registry.getMetricsAsArray(); + void asArray; + + const counterSnapshot: MetricObjectWithValues> = + await counter.get(); + void counterSnapshot; + + const named: MetricValueWithName = { + value: 1, + labels: {}, + metricName: 'typescript_test_counter', + }; + void named; +} +void metricObjectTypesAreExported;