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 optional typing to the useArgs hook #18715

Closed

Conversation

Gabriel5934
Copy link

Issue: args returned by the useArgs hook cannot be typed.

What I did

Added a generic type to the useArgs function so the returned args object type can match the actual component props type.

Also added default value to the generic of {[key]: string} so the hook still works as intended without passing a generic to useArgs.

@nx-cloud
Copy link

nx-cloud bot commented Jul 14, 2022

☁️ Nx Cloud Report

CI is running/has finished running commands for commit e566adb. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@yannbf
Copy link
Member

yannbf commented Jul 18, 2022

Hey @Gabriel5934 thanks for your contribution!

I wonder if this contribution is similar to #18122. cc @shilman

@@ -442,7 +442,11 @@ export function useAddonState<S>(addonId: string, defaultState?: S) {
return useSharedState<S>(addonId, defaultState);
}

export function useArgs(): [Args, (newArgs: Args) => void, (argNames?: string[]) => void] {
export function useArgs<Args extends Record<string, any> = { [key: string]: any }>(): [
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export function useArgs<Args extends Record<string, any> = { [key: string]: any }>(): [
export function useArgs<TArgs extends Args = Args>(): [

Copy link
Member

Choose a reason for hiding this comment

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

We should have an Args type that we can use instead of Record<string, any>

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

@Gabriel5934 Thanks so much for your contribution and patience on this one. We just got around to looking at it as a group and here's the collective wisdom on this one:

This is the useArgs that is exposed in the manager (Storybook's UI), not the preview, so the type will change for every single components' story. Therefore making the type generic might not be so useful? There is another PR that does the same for the preview (Storybook's canvas for displaying user components), and perhaps that was what you were intending?

@ndelangen
Copy link
Member

@kasperpeulen you're working on an huge effort to improve the situation, and that includes what this PR is doing, correct?

WDYT we should do with this PR?
Is there some way we can have @Gabriel5934 's commits count?

@@ -442,7 +442,11 @@ export function useAddonState<S>(addonId: string, defaultState?: S) {
return useSharedState<S>(addonId, defaultState);
}

export function useArgs(): [Args, (newArgs: Args) => void, (argNames?: string[]) => void] {
export function useArgs<Args extends Record<string, any> = { [key: string]: any }>(): [
Copy link
Member

Choose a reason for hiding this comment

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

Ow I now just read @shilman 's comment and looked at the changed code.

I'm afraid we can't do this. @Gabriel5934
The reason is that it would make the code less sound.. Though it's nice to get auto-completion, it's really dangerous to tell yourself you'll receive args of type X, but then at runtime you get a completely different object.

Now typescript tells you everything is fine, but in actuality it will likely throw at runtime.
And in this case when the code throws... it's a fatal problem for the storybook manager code.

We really appreciate your time, energy and effort that you put into this PR, but unfortunately we can't merge it :(
I hope you understand our reasoning. ❤️

@ndelangen ndelangen closed this Oct 5, 2022
@ndelangen ndelangen assigned ndelangen and unassigned kasperpeulen Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants