[skip-ci] POC: typings for expressions AST builder#62334
[skip-ci] POC: typings for expressions AST builder#62334lukeelmers wants to merge 4 commits intoelastic:masterfrom
Conversation
| builder.createFunction('visualization', { | ||
| index: 'foo', | ||
| visConfig: '{}', | ||
| }); |
There was a problem hiding this comment.
This will enforce that the provided args match the arguments interface for the visualization function
| // Add esaggs function to ExpressionFunctionDefinitions | ||
| declare module '../../../../expressions/public' { | ||
| interface ExpressionFunctionDefinitions { | ||
| esaggs: ExpressionFunctionEsaggs; | ||
| } | ||
| } |
There was a problem hiding this comment.
The catch is that each plugin registering an expression function will need to do this. If the function is available on the server they will need to declare it there too. (Or if we end up moving toward common being a first-class citizen in plugins, they'll only have to do this once)
| export interface ExpressionFunctionDefinitions { | ||
| // The property name should match the name of your function. | ||
| kibana_context: ExpressionFunctionKibanaContext; | ||
|
|
There was a problem hiding this comment.
All of the other functions provided by the expressions plugin would need to be added here too
| createFunction<K extends keyof ExpressionFunctionDefinitions>( | ||
| name: K, | ||
| args: ExpressionFunctionDefinitions[K] extends ExpressionFunctionDefinition< | ||
| infer Name, | ||
| infer Input, | ||
| infer Arguments, | ||
| infer Output, | ||
| infer Context | ||
| > | ||
| ? Arguments | ||
| : never |
There was a problem hiding this comment.
This is where the magic happens. We are looking at the string provided in the first argument, and using it as the index for ExpressionFunctionDefinitions. Then we infer each of the params which were provided to the original ExpressionFunctionDefinition interface.
|
Closing as a final approach was worked out and merged as a part of #64395 |
Do not merge -- this is just an example
This shows a way you could enforce expression function types in an AST builder.
It requires each plugin registering an expression function to also add a property with the same function name to a shared
ExpressionFunctionDefinitionsinterface which is exported fromsrc/plugins/expressions.Then in the AST builder's
createFunctionmethod, we can use the provided function name to infer the correct interface(s) fromExpressionFunctionDefinitions