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

Types: Improve DocumentActionComponent usage and extendability #4863

Open
roch-numbered opened this issue Aug 23, 2023 · 0 comments
Open

Types: Improve DocumentActionComponent usage and extendability #4863

roch-numbered opened this issue Aug 23, 2023 · 0 comments

Comments

@roch-numbered
Copy link

Is your feature request related to a problem? Please describe.
When developing a plugin, new actions aren't accepted by the typechecker.

my-plugin/plugin.tsx

import { MyPluginAction } from './actions/MyPluginAction';

export const myPlugin = definePlugin({
  name: 'my-plugin',
  document: {
    actions: (prev) => {
      // shortened version of the error
      // ❌ impossible to assign type 'string' to type '"delete" | "discardChanges" | "duplicate" | "restore" | "publish" | "unpublish" | undefined'
      prev.push(MyPluginAction);

      return prev;
    },
  }
});

my-plugin/actions/MyPluginAction.tsx

export const MyPluginAction: DocumentActionComponent = props => {};

MyPluginAction.action = 'myPlugin';

I can instead of DocumentActionComponent, simply use DocumentActionProps in the props object but I'll simply get a different error.

Describe the solution you'd like
Use keys of an interface instead of an union of string literals for DocumentActionComponent['action'].

packages/sanity/src/core/config/document/actions.ts

export interface DocumentActionKeys {
  delete: never
  discardChanges: never
  duplicate: never
  restore: never
  publish: never
  unpublish: never
}

export interface DocumentActionComponent extends ActionComponent<DocumentActionProps> {
  action?: keyof DocumentActionKeys
}

This will allows users to use declaration merging and add there own action names.

sanity.d.ts

import 'sanity'

// redeclare the sanity module
declare module 'sanity' {
  export interface DocumentActionKeys {
    myPlugin: never
  }
}
image

Describe alternatives you've considered
I've tried to use declaration merging to rewrite the type of DocumentActionComponent['action'] but that's not how TypeScript works.

Thx, cheers! ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant