-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Alerting] Encourage type safe usage of Alerting #86623
[Alerting] Encourage type safe usage of Alerting #86623
Conversation
* master: (48 commits) Fix request with disabled aggregation (elastic#85696) [Security Solution][Detections][Threshold Rules] Threshold Rule Bug Fixes (elastic#84918) Removed a possibility to define two different names for Alert types on API and UI level. (elastic#86236) Bump Node.js from version 14.15.2 to 14.15.3 (elastic#86593) [index patterns] Fleep app - Keep saved object field list until field caps provides fields (elastic#85370) [Security Solutions] fix timeline tabs + layout (elastic#86581) Upgrade to hapi version 20 (elastic#85406) App Services: Remove remaining uiActions, expressions, data, embeddable circular dependencies. (elastic#82791) Rename chartLibrary setting to legacyChartsLibrary (elastic#86529) [CI] TeamCity updates (elastic#85843) [Maps] Use Json for mvt-tests (elastic#86492) [Rollup Jobs] Added autofocus to cron editor (elastic#86324) [Monitoring][Alerting] CCR read exceptions alert (elastic#85908) [CI] Bump memory for main CI workers (elastic#86541) Explicitly set Elasticsearch heap size during CI and local development (elastic#86513) [App Search] Updates to results on the documents view (elastic#86181) [Discover] Change default sort handling (elastic#85561) [App Search] Convert DocumentCreationModal to DocumentCreationFlyout (elastic#86508) [App Search] Sample Engines should have access to the Crawler (elastic#86502) Fixed duplication of create new modal (elastic#86489) ...
@@ -32,7 +32,7 @@ export interface ConstructorOptions { | |||
|
|||
export interface RegistryAlertType | |||
extends Pick< | |||
NormalizedAlertType, | |||
UntypedNormalizedAlertType, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't have mixed types in the Map
, so we have to string these when we set them, and rely on the user to provide the right type when they get them.
export type UntypedNormalizedAlertType = NormalizedAlertType< | ||
AlertTypeParams, | ||
AlertTypeState, | ||
AlertInstanceState, | ||
AlertInstanceContext | ||
>; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By Untyped...
what we mean is that we down-cast the types to their base type, but it sounded clearer to me than BaseTypeNormalizedAlertType
or DownCastTypedNormalizedAlertType
Params extends AlertTypeParams, | ||
State extends AlertTypeState, | ||
InstanceState extends AlertInstanceState, | ||
InstanceContext extends AlertInstanceContext |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed defaults so we force the developers to be explicit about the types they expect.
This should encourage them to use real types rather than just Record<string, whatever>
} | ||
|
||
public async get({ id }: { id: string }): Promise<SanitizedAlert> { | ||
public async get<Params extends AlertTypeParams = never>({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using never
here felt right - we don't force them to specify the type, unless they want to use the field, in which case they'll have to be explicit.
alertInterval: string; | ||
alertThrottle: string; | ||
setAlertParams: (property: string, value: any) => void; | ||
setAlertProperty: <Key extends keyof Alert>(key: Key, value: Alert[Key] | null) => void; | ||
setAlertProperty: <Key extends keyof Alert>(key: Key, value: any) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out this type was wrong the whole time, and had an underlying any
.
At least now it's explicit... but I'll still try to fix this before we merge.
Pinging @elastic/uptime (Team:uptime) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for Stack Monitoring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Cleanup looks great.
💚 Build SucceededMetrics [docs]Async chunks
Distributable file count
Page load bundle
History
To update your PR or re-run it, just comment with: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This PR encourages type safe usage of the Alerting framework by replacing the current default Params/State/InstanceState/InstanceContext types (which are `AlertTypeParams`/`AlertTypeState`/etc.) with `never`. This means that code can continue to omit the specific types for these fields, as long as they aren't referenced. Once an alert developer wishes to actually reference the parameters (or state/context), then they have to specify the type. This PR also changed the typing of the `AlertTypeParams` and `AlertTypeState` from `Record<string, any>` to `Record<string, unknown>`, to ensure that where these catch-all types are used they will at least enforce `unknown` rather than `any`. This change broke some usage in both @elastic/kibana-alerting-services plugins, but also other plugins in the Stack/Solutions. I tried to fix these where I could, but some of these require new types and refactoring in other teams' code, which I decided is best done by the team who own and maintain that code - I've added explicit `TODO` comments in all of these places, describing the required fix. This PR also introduced a Generics based typing for the `Alert` type so that the `params` field can be typed as something other than `AlertTypeParams`.
This PR encourages type safe usage of the Alerting framework by replacing the current default Params/State/InstanceState/InstanceContext types (which are `AlertTypeParams`/`AlertTypeState`/etc.) with `never`. This means that code can continue to omit the specific types for these fields, as long as they aren't referenced. Once an alert developer wishes to actually reference the parameters (or state/context), then they have to specify the type. This PR also changed the typing of the `AlertTypeParams` and `AlertTypeState` from `Record<string, any>` to `Record<string, unknown>`, to ensure that where these catch-all types are used they will at least enforce `unknown` rather than `any`. This change broke some usage in both @elastic/kibana-alerting-services plugins, but also other plugins in the Stack/Solutions. I tried to fix these where I could, but some of these require new types and refactoring in other teams' code, which I decided is best done by the team who own and maintain that code - I've added explicit `TODO` comments in all of these places, describing the required fix. This PR also introduced a Generics based typing for the `Alert` type so that the `params` field can be typed as something other than `AlertTypeParams`.
* master: (36 commits) update apm index pattern (elastic#86739) [Visualizations] Remove vis_default_editor - visualize plugins cyclic dependencies (elastic#85422) [ML] Fix alignment of values in data frame analytics results view badges (elastic#86621) [Visualizations] Remove charts - editor plugins cyclic dependencies (elastic#84887) fixing blank page (elastic#86640) Update dependency vega to ^5.17.1 (elastic#86715) [Monitoring] Convert Kibana-related server files that read from _source to typescript (elastic#86364) Uses @elastic/elasticsearch-canary (elastic#86398) [CI] Removes script previously used for Karma (elastic#86412) [build] Remove grunt checkPlugins task (elastic#85852) [build] Remove grunt docker:docs task (elastic#85848) [ML] Add doc link for classification AUC ROC evaluation (elastic#86660) [ML] Edits saved object synchronization message (elastic#86664) Uses the new es client in canvas usage collector's fetch methods (elastic#86668) [ML] Support legacy watcher URL (elastic#86661) [ML] Fix Single Metric Viewer y domain extending beyond the visible focus area (elastic#86655) Migrates search telemetry usage collector es client from legacy to new (elastic#86597) [Alerting] Encourage type safe usage of Alerting (elastic#86623) Migrates kql_telemetry usage collector es client (elastic#86585) [ML] Fix time range adjustment for the swim lane causing the infinite loop update (elastic#86461) ...
Summary
Closes #74897
This PR encourages type safe usage of the Alerting framework by replacing the current default Params/State/InstanceState/InstanceContext types (which are
AlertTypeParams
/AlertTypeState
/etc.) withnever
.This means that code can continue to omit the specific types for these fields, as long as they aren't referenced.
Once an alert developer wishes to actually reference the parameters (or state/context), then they have to specify the type.
This PR also changed the typing of the
AlertTypeParams
andAlertTypeState
fromRecord<string, any>
toRecord<string, unknown>
, to ensure that where these catch-all types are used they will at least enforceunknown
rather thanany
.This change broke some usage in both @elastic/kibana-alerting-services plugins, but also other plugins in the Stack/Solutions. I tried to fix these where I could, but some of these require new types and refactoring in other teams' code, which I decided is best done by the team who own and maintain that code - I've added explicit
TODO
comments in all of these places, describing the required fix.This PR also introduced a Generics based typing for the
Alert
type so that theparams
field can be typed as something other thanAlertTypeParams
.Checklist
Delete any items that are not applicable to this PR.
[ ] Any text added follows EUI's writing guidelines, uses sentence case text and includes i18n support[ ] Documentation was added for features that require explanation or tutorials[ ] Any UI touched in this PR is usable by keyboard only (learn more about keyboard accessibility)[ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: FF, Chrome)[ ] This renders correctly on smaller devices using a responsive layout. (You can test this in your browser)[ ] This was checked for cross-browser compatibilityFor maintainers