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

Expose types from useAPI #1525

Open
MitchLillie opened this issue Nov 13, 2023 · 0 comments
Open

Expose types from useAPI #1525

MitchLillie opened this issue Nov 13, 2023 · 0 comments

Comments

@MitchLillie
Copy link
Contributor

MitchLillie commented Nov 13, 2023

Overview

We need a way to directly expose types for the APIs returned by useApi, for example navigation.navigate or saveMetafields.

Context

          @olavoasantos, @MitchLillie, there's still no easy way to expose this to the extensions (nor to web, for that matter).

I managed to resolve the type through some TS gymnastics like:

type ExtractElementType<T> = T extends readonly (infer ElementType)[]
  ? ElementType
  : never;

type ApiForRenderExtension = ReturnType<
  typeof useApi<'admin.settings.order-routing-rule.render'>
>;

type ExtractMethod<T extends keyof ApiForRenderExtension> =
  ApiForRenderExtension[T];

type SaveMetafields = ExtractMethod<'saveMetafields'>;

type MetafieldChange = ExtractElementType<Parameters<SaveMetafields>[0]>;

I like how the types are clean and properly derived from useApi(TARGET), but wondering if we'd want to expose some of the helpers that already exist in this repo as well in order to make this slightly simpler... or to allow for some sort of additional types 🤔

Normally one wouldn't need this, but as things grow, types might be needed in files that do not call useApi making some of this necessary 🤔

Originally posted by @jbalsas in #1472 (comment)

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