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

Update fieldProps.ts #5902

Merged
merged 1 commit into from Mar 19, 2024
Merged

Update fieldProps.ts #5902

merged 1 commit into from Mar 19, 2024

Conversation

shixish
Copy link
Contributor

@shixish shixish commented Mar 5, 2024

Description

What changes are introduced?

I changed some TS typing information related to custom schema component props.

Why are these changes introduced?

It intends to fix some TS type errors.

What issue(s) does this solve? (with link, if possible)

The changes were to address something like this:

defineField({
  name: 'path',
  type: 'slug',
  components: {
    field: (props: ObjectFieldProps<SlugValue>) => {
      const doSomething = (value?: string) => {
        // something
      }
      return (
        <Stack padding={0} space={[3, 3, 4, 5]}>
          {props.renderDefault(props)}
          <Button disabled={!props.value} onClick={() => doSomething(props.value?.current)}>example</Button>
        </Stack>
      );
    }
  }
}),

I initially tried using the broader FieldProps type for props parameter but trying to use props.value?.current gives this TS error:

Property 'current' does not exist on type 'string | number | boolean | SlugValue | CrossDatasetReferenceValue | FileValue | GeopointValue | ImageValue | Record<...> | Reference | unknown[]'.
  Property 'current' does not exist on type 'string'.ts(2339)

So I changed it to use the more specific ObjectFieldProps<SlugValue> props value, however this had the issue where the current value is set as unknown even though it's type should be known via SlugValue type.

I changed the ObjectFieldProps definition to apply the T template variable type to the value object so that it's type can be inferred based on the provided Object type (SlugValue in this case).

What to review

The changes only apply to TS error messages related to custom schema components' field props. The example above should be good enough to reproduce the issue, just adding this to your schema. The example above includes some components from @sanity/ui as is described in the docs.

Testing

I didn't really test beyond my specific use case with ObjectFieldProps<SlugValue>. Someone should probably have a look that these changes apply properly to the rest of the FieldProps generic type options:

  | ObjectFieldProps
  | ObjectFieldProps<CrossDatasetReferenceValue>
  | ObjectFieldProps<FileValue>
  | ObjectFieldProps<GeopointValue>
  | ObjectFieldProps<ImageValue>
  | ObjectFieldProps<ReferenceValue>
  | ObjectFieldProps<SlugValue>

Apologies if I've done this wrong. I don't typically send out pull requests like this 🤷 ✌️

@shixish shixish requested a review from a team as a code owner March 5, 2024 01:02
@shixish shixish requested review from binoy14 and removed request for a team March 5, 2024 01:02
Copy link

vercel bot commented Mar 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
performance-studio ✅ Ready (Inspect) Visit Preview Mar 5, 2024 1:06am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Mar 5, 2024 1:06am

Copy link

vercel bot commented Mar 5, 2024

@shixish is attempting to deploy a commit to the Sanity Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@ricokahler ricokahler left a comment

Choose a reason for hiding this comment

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

Thanks for this. This seems very sensible! Adding @bjoerge as a reviewer just to be sure.

@ricokahler ricokahler requested a review from bjoerge March 19, 2024 15:52
@binoy14 binoy14 enabled auto-merge March 19, 2024 16:15
@binoy14 binoy14 added this pull request to the merge queue Mar 19, 2024
Merged via the queue into sanity-io:next with commit 10d86e0 Mar 19, 2024
23 of 32 checks passed
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

Successfully merging this pull request may close these issues.

None yet

4 participants