Skip to content

Commit

Permalink
More strict plugin type definitions (elastic#205232)
Browse files Browse the repository at this point in the history
Stricter defaults for plugin types: `Plugin` and `CoreSetup` now have
empty objects as defaults instead of `object` which is assignable to
anything basically. This catches some type errors, but my motivation for
this is to allow something like:

```ts
function createPlugin ():Plugin<MySetupContract, MyStartContract, MySetupDependencies, MyStartDependencies> {
	return {
		// look ma, no additional typing necessary
		setup ( coreSetup, pluginsSetup ) {
		},
		start ( coreStart, pluginsStart ) {
		}
	}
}
```
  • Loading branch information
dgieselaar authored and CAWilson94 committed Jan 10, 2025
1 parent 2627347 commit 7d04675
Show file tree
Hide file tree
Showing 30 changed files with 133 additions and 59 deletions.
5 changes: 4 additions & 1 deletion examples/eso_model_version_example/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export interface EsoModelVersionExamplePluginsStart {
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
}

export class EsoModelVersionExample implements Plugin<void, void> {
export class EsoModelVersionExample
implements
Plugin<void, void, EsoModelVersionExamplePluginSetup, EsoModelVersionExamplePluginsStart>
{
public setup(
core: CoreSetup<EsoModelVersionExamplePluginsStart>,
plugins: EsoModelVersionExamplePluginSetup
Expand Down
2 changes: 1 addition & 1 deletion src/core/packages/lifecycle/browser/src/core_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import type { CoreStart } from './core_start';
* navigation in the generated docs until there's a fix for
* https://github.com/Microsoft/web-build-tools/issues/1237
*/
export interface CoreSetup<TPluginsStart extends object = object, TStart = unknown> {
export interface CoreSetup<TPluginsStart extends Record<string, any> = {}, TStart = unknown> {
/** {@link AnalyticsServiceSetup} */
analytics: AnalyticsServiceSetup;
/** {@link ApplicationSetup} */
Expand Down
2 changes: 1 addition & 1 deletion src/core/packages/lifecycle/server/src/core_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import type { CoreStart } from './core_start';
* consuming {@link Plugin}'s `TStart` type. Used by `getStartServices`.
* @public
*/
export interface CoreSetup<TPluginsStart extends object = object, TStart = unknown> {
export interface CoreSetup<TPluginsStart extends Record<string, any> = {}, TStart = unknown> {
/** {@link AnalyticsServiceSetup} */
analytics: AnalyticsServiceSetup;
/** {@link CapabilitiesSetup} */
Expand Down
4 changes: 2 additions & 2 deletions src/core/packages/plugins/browser/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type { CoreStart, CoreSetup } from '@kbn/core-lifecycle-browser';
export interface Plugin<
TSetup = void,
TStart = void,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
TPluginsSetup extends Record<string, any> = never,
TPluginsStart extends Record<string, any> = never
> {
setup(core: CoreSetup<TPluginsStart, TStart>, plugins: TPluginsSetup): TSetup;

Expand Down
8 changes: 4 additions & 4 deletions src/core/packages/plugins/browser/src/plugin_initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import type { Plugin } from './plugin';
* @public
*/
export type PluginInitializer<
TSetup,
TStart,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
TSetup = void,
TStart = void,
TPluginsSetup extends Record<string, any> = never,
TPluginsStart extends Record<string, any> = never
> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/packages/plugins/server-internal/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class PluginWrapper<
* is the contract returned by the dependency's `setup` function.
*/
public setup(
setupContext: CoreSetup<TPluginsStart> | CorePreboot,
setupContext: CoreSetup<TPluginsStart, TStart> | CorePreboot,
plugins: TPluginsSetup
): TSetup | Promise<TSetup> {
if (!this.instance) {
Expand All @@ -109,7 +109,7 @@ export class PluginWrapper<
return this.instance.setup(setupContext as CorePreboot, plugins);
}

return this.instance.setup(setupContext as CoreSetup, plugins);
return this.instance.setup(setupContext as CoreSetup<TPluginsStart, TStart>, plugins);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/core/packages/plugins/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ export interface PrebootPlugin<TSetup = void, TPluginsSetup extends object = obj
export interface Plugin<
TSetup = void,
TStart = void,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
TPluginsSetup extends Record<string, any> = {},
TPluginsStart extends Record<string, any> = {}
> {
setup(core: CoreSetup, plugins: TPluginsSetup): TSetup;
setup(core: CoreSetup<TPluginsStart, TStart>, plugins: TPluginsSetup): TSetup;

start(core: CoreStart, plugins: TPluginsStart): TStart;

Expand Down Expand Up @@ -451,10 +451,10 @@ export interface PluginInitializerContext<ConfigSchema = unknown> {
* @public
*/
export type PluginInitializer<
TSetup,
TStart,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
TSetup = void,
TStart = void,
TPluginsSetup extends Record<string, any> = never,
TPluginsStart extends Record<string, any> = never
> = (
core: PluginInitializerContext
) => Promise<
Expand Down
4 changes: 3 additions & 1 deletion src/platform/plugins/private/maps_ems/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ export class MapsEmsPlugin implements Plugin<MapsEmsPluginServerSetup> {
};
}

public start() {}
public start() {
return {};
}
}
4 changes: 2 additions & 2 deletions src/platform/plugins/shared/dashboard/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ interface StartDeps {
}

export class DashboardPlugin
implements Plugin<DashboardPluginSetup, DashboardPluginStart, SetupDeps>
implements Plugin<DashboardPluginSetup, DashboardPluginStart, SetupDeps, StartDeps>
{
private readonly logger: Logger;

constructor(private initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
}

public setup(core: CoreSetup<StartDeps>, plugins: SetupDeps) {
public setup(core: CoreSetup<StartDeps, DashboardPluginStart>, plugins: SetupDeps) {
this.logger.debug('dashboard: Setup');

core.savedObjects.registerType(
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
CoreStart,
KibanaRequest,
Logger,
Plugin,
PluginInitializerContext,
SharedGlobalConfig,
StartServicesAccessor,
Expand Down Expand Up @@ -121,7 +120,7 @@ export interface SearchRouteDependencies {
globalConfig$: Observable<SharedGlobalConfig>;
}

export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
export class SearchService {
private readonly aggsService = new AggsService();
private readonly searchSourceService = new SearchSourceService();
private searchStrategies: StrategyMap = {};
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/unified_search/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ export interface UnifiedSearchServerPluginSetupDependencies {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface UnifiedSearchServerPluginStartDependencies {}

export class UnifiedSearchServerPlugin implements Plugin<UnifiedSearchServerPluginSetup> {
export class UnifiedSearchServerPlugin
implements
Plugin<
UnifiedSearchServerPluginSetup,
UnifiedSearchServerPluginStart,
UnifiedSearchServerPluginSetupDependencies,
UnifiedSearchServerPluginStartDependencies
>
{
private readonly autocompleteService: AutocompleteService;

constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/vis_types/timeseries/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ export interface Framework {
getEsShardTimeout: () => Promise<number>;
}

export class VisTypeTimeseriesPlugin implements Plugin<VisTypeTimeseriesSetup> {
export class VisTypeTimeseriesPlugin
implements
Plugin<
VisTypeTimeseriesSetup,
void,
VisTypeTimeseriesPluginSetupDependencies,
VisTypeTimeseriesPluginStartDependencies
>
{
constructor(private readonly initializerContext: PluginInitializerContext) {
this.initializerContext = initializerContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ interface MessageBody {
messages: Message[];
}

export class GenAiStreamingResponseExamplePlugin implements Plugin<void, void> {
export class GenAiStreamingResponseExamplePlugin
implements Plugin<void, void, {}, GenAiStreamingResponseExamplePluginStart>
{
public setup({ http, getStartServices }: CoreSetup<GenAiStreamingResponseExamplePluginStart>) {
const router = http.createRouter();

Expand Down
2 changes: 1 addition & 1 deletion x-pack/examples/screenshotting_example/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface StartDeps {
screenshotting: ScreenshottingStart;
}

export class ScreenshottingExamplePlugin implements Plugin<void, void> {
export class ScreenshottingExamplePlugin implements Plugin<void, void, {}, StartDeps> {
setup({ http, getStartServices }: CoreSetup<StartDeps>) {
const router = http.createRouter();

Expand Down
2 changes: 1 addition & 1 deletion x-pack/platform/plugins/private/canvas/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface PluginsStart {
data: DataPluginStart;
}

export class CanvasPlugin implements Plugin {
export class CanvasPlugin implements Plugin<void, void, PluginsSetup, PluginsStart> {
private readonly logger: Logger;

constructor(public readonly initializerContext: PluginInitializerContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { registerWithCustomIntegrations } from './register_custom_integration';
import { routes } from './routes';
import type { ConfigSchema } from '../common/app';

export class DataVisualizerPlugin implements Plugin {
export class DataVisualizerPlugin implements Plugin<void, void, SetupDeps, StartDeps> {
private readonly _logger: Logger;

constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
Expand Down
7 changes: 5 additions & 2 deletions x-pack/platform/plugins/shared/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ const includedHiddenTypes = [
CONNECTOR_TOKEN_SAVED_OBJECT_TYPE,
];

export class ActionsPlugin implements Plugin<PluginSetupContract, PluginStartContract> {
export class ActionsPlugin
implements
Plugin<PluginSetupContract, PluginStartContract, ActionsPluginsSetup, ActionsPluginsStart>
{
private readonly logger: Logger;
private readonly actionsConfig: ActionsConfig;
private taskRunnerFactory?: TaskRunnerFactory;
Expand Down Expand Up @@ -230,7 +233,7 @@ export class ActionsPlugin implements Plugin<PluginSetupContract, PluginStartCon
}

public setup(
core: CoreSetup<ActionsPluginsStart>,
core: CoreSetup<ActionsPluginsStart, PluginStartContract>,
plugins: ActionsPluginsSetup
): PluginSetupContract {
this.licenseState = new LicenseState(plugins.licensing.license$);
Expand Down
11 changes: 10 additions & 1 deletion x-pack/platform/plugins/shared/dataset_quality/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ import { getDatasetQualityServerRouteRepository } from './routes';
import { registerRoutes } from './routes/register_routes';
import { DatasetQualityRouteHandlerResources } from './routes/types';
import {
DatasetQualityPluginSetup,
DatasetQualityPluginSetupDependencies,
DatasetQualityPluginStart,
DatasetQualityPluginStartDependencies,
} from './types';

export class DatasetQualityServerPlugin implements Plugin {
export class DatasetQualityServerPlugin
implements
Plugin<
DatasetQualityPluginSetup,
DatasetQualityPluginStart,
DatasetQualityPluginSetupDependencies,
DatasetQualityPluginStartDependencies
>
{
private readonly logger: Logger;
private readonly dataTelemetryService: DataTelemetryService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,36 @@ import type {
Logger,
CustomRequestHandlerContext,
} from '@kbn/core/server';
import type { PluginStartContract as ActionsPluginsStart } from '@kbn/actions-plugin/server/plugin';
import { MINIMUM_LICENSE_TYPE } from '../common/constants';
import { registerRoutes } from './routes';
import type {
IntegrationAssistantPluginSetup,
IntegrationAssistantPluginStart,
IntegrationAssistantPluginStartDependencies,
IntegrationAssistantPluginSetupDependencies,
} from './types';
import { parseExperimentalConfigValue } from '../common/experimental_features';
import { IntegrationAssistantConfigType } from './config';

export type IntegrationAssistantRouteHandlerContext = CustomRequestHandlerContext<{
integrationAssistant: {
getStartServices: CoreSetup<{
actions: ActionsPluginsStart;
}>['getStartServices'];
getStartServices: CoreSetup<
IntegrationAssistantPluginStartDependencies,
IntegrationAssistantPluginStart
>['getStartServices'];
isAvailable: () => boolean;
logger: Logger;
};
}>;

export class IntegrationAssistantPlugin
implements Plugin<IntegrationAssistantPluginSetup, IntegrationAssistantPluginStart>
implements
Plugin<
IntegrationAssistantPluginSetup,
IntegrationAssistantPluginStart,
IntegrationAssistantPluginSetupDependencies,
IntegrationAssistantPluginStartDependencies
>
{
private readonly logger: Logger;
private readonly config: IntegrationAssistantConfigType;
Expand All @@ -50,9 +57,7 @@ export class IntegrationAssistantPlugin
}

public setup(
core: CoreSetup<{
actions: ActionsPluginsStart;
}>
core: CoreSetup<IntegrationAssistantPluginStartDependencies, IntegrationAssistantPluginStart>
): IntegrationAssistantPluginSetup {
core.http.registerRouteHandlerContext<
IntegrationAssistantRouteHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
ActionsClientSimpleChatModel,
} from '@kbn/langchain/server';
import type { LicensingPluginSetup, LicensingPluginStart } from '@kbn/licensing-plugin/server';
import type {
PluginStartContract as ActionsPluginStart,
PluginSetupContract as ActionsPluginSetup,
} from '@kbn/actions-plugin/server/plugin';
import { ESProcessorItem, SamplesFormat } from '../common';

export interface IntegrationAssistantPluginSetup {
Expand All @@ -22,9 +26,11 @@ export interface IntegrationAssistantPluginStart {}

export interface IntegrationAssistantPluginSetupDependencies {
licensing: LicensingPluginSetup;
actions: ActionsPluginSetup;
}
export interface IntegrationAssistantPluginStartDependencies {
licensing: LicensingPluginStart;
actions: ActionsPluginStart;
}

export interface SimplifiedProcessor {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/platform/plugins/shared/maps/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { registerIntegrations } from './register_integrations';
import { StartDeps, SetupDeps } from './types';
import { MapsStorage } from './content_management';

export class MapsPlugin implements Plugin {
export class MapsPlugin implements Plugin<void, void, SetupDeps, StartDeps> {
readonly _initializerContext: PluginInitializerContext<MapsXPackConfig>;
private readonly _logger: Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export interface ConnectorsPluginsStart {
actions: ActionsPluginSetupContract;
}

export class StackConnectorsPlugin implements Plugin<void, void> {
export class StackConnectorsPlugin
implements Plugin<void, void, ConnectorsPluginsSetup, ConnectorsPluginsStart>
{
private readonly logger: Logger;
private config: StackConnectorsConfigType;
readonly experimentalFeatures: ExperimentalFeatures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TaskTypeDictionary } from '../task_type_dictionary';
import { BackgroundTaskNode } from '../saved_objects/schemas/background_task_node';
import { BACKGROUND_TASK_NODE_SO_NAME } from '../saved_objects';
import { TaskManagerStartContract } from '..';
import { TaskManagerPluginsStart } from '../plugin';

export const TASK_ID = 'delete_inactive_background_task_nodes';
const TASK_TYPE = `task_manager:${TASK_ID}`;
Expand Down Expand Up @@ -40,7 +41,7 @@ export async function scheduleDeleteInactiveNodesTaskDefinition(

export function registerDeleteInactiveNodesTaskDefinition(
logger: Logger,
coreStartServices: () => Promise<[CoreStart, TaskManagerStartContract, unknown]>,
coreStartServices: () => Promise<[CoreStart, TaskManagerPluginsStart, TaskManagerStartContract]>,
taskTypeDictionary: TaskTypeDictionary
) {
taskTypeDictionary.registerTaskDefinitions({
Expand All @@ -53,7 +54,7 @@ export function registerDeleteInactiveNodesTaskDefinition(

export function taskRunner(
logger: Logger,
coreStartServices: () => Promise<[CoreStart, TaskManagerStartContract, unknown]>
coreStartServices: () => Promise<[CoreStart, TaskManagerPluginsStart, TaskManagerStartContract]>
) {
return () => {
return {
Expand Down
Loading

0 comments on commit 7d04675

Please sign in to comment.