1- import type { GrafanaCondition } from '../grafana' ;
1+ import type {
2+ GrafanaCondition ,
3+ GrafanaEvaluatorType ,
4+ GrafanaOperatorType ,
5+ GrafanaReducerType ,
6+ } from '../grafana' ;
27
38class Condition {
4- private state : GrafanaCondition ;
5- private _evaluator : { params : any [ ] ; type : string } ;
6- private _operator : { type : string } ;
7- private _query : { params : string [ ] } ;
8- private _reducer : { params : any [ ] ; type : string } ;
9+ private state : Partial < GrafanaCondition > ;
10+ private _evaluator : GrafanaCondition [ 'evaluator' ] ;
11+ private _operator : GrafanaCondition [ 'operator' ] ;
12+ private _query : GrafanaCondition [ 'query' ] ;
13+ private _reducer : GrafanaCondition [ 'reducer' ] ;
914 constructor ( opts : Partial < GrafanaCondition > = { } ) {
10- // @ts -expect-error todo: should fields be optional?
1115 this . state = { } ;
1216
1317 this . _evaluator = {
@@ -32,7 +36,12 @@ class Condition {
3236 this . state [ opt ] = opts [ opt ] ;
3337 } ) ;
3438 }
35- withEvaluator ( value , type ) {
39+ withEvaluator ( value : ( string | number ) [ ] , type : 'within_range' ) : Condition ;
40+ withEvaluator ( value : string | number , type : 'gt' | 'lt' ) : Condition ;
41+ withEvaluator (
42+ value : ( string | number ) | ( string | number ) [ ] ,
43+ type : GrafanaEvaluatorType
44+ ) {
3645 const types = [ 'gt' , 'lt' , 'within_range' ] ;
3746
3847 if ( ! types . includes ( type ) ) {
@@ -42,15 +51,15 @@ class Condition {
4251 this . _evaluator . type = type ;
4352
4453 if ( [ 'gt' , 'lt' ] . includes ( type ) ) {
45- this . _evaluator . params = [ value ] ;
54+ this . _evaluator . params = [ value as string ] ;
4655 } else if ( Array . isArray ( value ) ) {
4756 this . _evaluator . params = value ;
4857 }
4958
5059 return this ;
5160 }
52- withOperator ( operator ) {
53- const types = [ 'and' , 'or' ] ;
61+ withOperator ( operator : GrafanaOperatorType ) {
62+ const types = [ 'and' , 'or' ] as const ;
5463
5564 if ( ! types . includes ( operator ) ) {
5665 throw Error ( `Operator must be one of [${ types . toString } ]` ) ;
@@ -67,7 +76,7 @@ class Condition {
6776 this . _operator . type = 'and' ;
6877 return this ;
6978 }
70- onQuery ( query , duration , from ) {
79+ onQuery ( query : string , duration ?: string , from ?: string ) {
7180 if ( typeof query !== 'string' ) {
7281 throw Error (
7382 'Query identifier must be a string. eg. "A" or "B", etc...'
@@ -79,7 +88,7 @@ class Condition {
7988
8089 return this ;
8190 }
82- withReducer ( type ) {
91+ withReducer ( type : GrafanaReducerType ) {
8392 const types = [
8493 'min' ,
8594 'max' ,
@@ -89,7 +98,7 @@ class Condition {
8998 'last' ,
9099 'median' ,
91100 'diff' ,
92- ] ;
101+ ] satisfies GrafanaReducerType [ ] ;
93102
94103 if ( ! types . includes ( type ) ) {
95104 throw Error ( `Reducer has to be one of [${ types . toString ( ) } ]` ) ;
@@ -98,7 +107,7 @@ class Condition {
98107 this . _reducer . type = type ;
99108 return this ;
100109 }
101- generate ( ) {
110+ generate ( ) : GrafanaCondition {
102111 return Object . assign (
103112 { } ,
104113 {
0 commit comments