Skip to content
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

add generic typings for SavedObjectMigrationFn #63943

Merged
merged 13 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A migration function for a [saved object type](./kibana-plugin-core-server.saved
<b>Signature:</b>

```typescript
export declare type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;
export declare type SavedObjectMigrationFn<InputProps = any, MigratedProps = any> = (doc: SavedObjectUnsanitizedDoc<InputProps>, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc<MigratedProps>;
```

## Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<b>Signature:</b>

```typescript
export declare type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable;
export declare type SavedObjectSanitizedDoc<T = unknown> = SavedObjectDoc<T> & Referencable;
```
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/migrations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import { SavedObjectsMigrationLogger } from './core/migration_logger';
*
* @public
*/
export type SavedObjectMigrationFn = (
doc: SavedObjectUnsanitizedDoc,
export type SavedObjectMigrationFn<InputProps = any, MigratedProps = any> = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably require updating the types of many migrations, but is it possible to default to unknown instead of any? I think that would encourage more plugins to type their migrations and make it explicit that their migration is dangerously untyped.

What do you think about calling these generic arguments InputAttributes and MigratedAttributes since they define the attributes property?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably require updating the types of many migrations, but is it possible to default to unknown instead of any

I wanted to avoid impacting plugin code with that change TBH, therefor the defaulting any. But it may be better to still do it now to encourage yet-to-migrate SO migrations to be explicitly typed.

What do you think about calling these generic arguments InputAttributes and MigratedAttributes since they define the attributes property?

Indeed better. I always mix up attributes and props terminology for SOs.

doc: SavedObjectUnsanitizedDoc<InputProps>,
context: SavedObjectMigrationContext
) => SavedObjectUnsanitizedDoc;
) => SavedObjectUnsanitizedDoc<MigratedProps>;

/**
* Migration context provided when invoking a {@link SavedObjectMigrationFn | migration handler}
Expand Down
8 changes: 4 additions & 4 deletions src/core/server/saved_objects/serialization/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export interface SavedObjectsRawDocSource {
* that future props are likely to be added. Migrations support this
* scenario out of the box.
*/
interface SavedObjectDoc {
attributes: any;
interface SavedObjectDoc<T = unknown> {
attributes: T;
id?: string; // NOTE: SavedObjectDoc is used for uncreated objects where `id` is optional
type: string;
namespace?: string;
Expand All @@ -75,7 +75,7 @@ interface Referencable {
*
* @public
*/
export type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial<Referencable>;
export type SavedObjectUnsanitizedDoc<T = unknown> = SavedObjectDoc<T> & Partial<Referencable>;

/** @public */
export type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable;
export type SavedObjectSanitizedDoc<T = unknown> = SavedObjectDoc<T> & Referencable;
4 changes: 2 additions & 2 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ export interface SavedObjectMigrationContext {
// Warning: (ae-forgotten-export) The symbol "SavedObjectUnsanitizedDoc" needs to be exported by the entry point index.d.ts
//
// @public
export type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;
export type SavedObjectMigrationFn<InputProps = any, MigratedProps = any> = (doc: SavedObjectUnsanitizedDoc<InputProps>, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc<MigratedProps>;

// @public
export interface SavedObjectMigrationMap {
Expand Down Expand Up @@ -1710,7 +1710,7 @@ export interface SavedObjectsAddToNamespacesOptions extends SavedObjectsBaseOpti
// Warning: (ae-forgotten-export) The symbol "Referencable" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable;
export type SavedObjectSanitizedDoc<T = unknown> = SavedObjectDoc<T> & Referencable;

// @public (undocumented)
export interface SavedObjectsBaseOptions {
Expand Down