question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Types have wrong `_type` key

See original GitHub issue

Let’s have the following simple schemas:

const fooImage = {
  type: 'image',
  title: 'Top-level Image Type',
  name: 'foo_image',
  fields: [
    {
      title: 'Title',
      name: 'title',
      type: 'string',
    },
  ],
};

const bar = {
  type: 'document',
  title: 'Bar',
  name: 'bar',
  fields: [
    {
      title: 'Nested Image Type',
      name: 'nestedImage',
      type: 'foo_image',
    }
  ],
};

When generated, the FooImage type is outputted like so:

export type FooImage = {
  _type: "image";
  asset: SanityAsset;
  /**
   * Title — `string`
   *
   *
   */
  title?: string;
};

However, the _type property is wrongly represented, as the _type of it should in fact be foo_image when referenced from the Bar document.

I don’t see any use case requiring to have the “base type” present in the _type property, but I may be wrong. Having the custom type there would better represent the actual Sanity API response format.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ricokahlercommented, Dec 18, 2020

I merged a fix that should fix it for images and files but I think slug is still an issue. I’ll address in a separate PR once I get some time.

1reaction
alexbchrcommented, Dec 16, 2020

Great! I took a quick try at it sooner today and it proved to be maybe more complex to do that what I initially thought. Unfortunately, I don’t have enough time today to dig more into it.

Just to add to the example, it is not only image which is problematic. This should be the behavior for all Types (image, file, slug, object and so on…). I did a very quick fix this morning (only for image and file types), here is the code (quite incomplete and a test still, but feel free to use it).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proposal: new "invalid" type to indicate custom invalid states
Proposal A new invalid type that is not assignable to or from any other types. This includes not being assignable to or from...
Read more >
How To Create Custom Types in TypeScript - DigitalOcean
In TypeScript, the syntax for creating custom types is to use the type keyword followed by the type name and then an assignment...
Read more >
Typescript Type 'string' is not assignable to type - Stack Overflow
1) The first solution is to define a type for the size and export it from the foo.ts. This is good if when...
Read more >
15 Typescript Mistakes To Avoid - SoftwareMill Tech Blog
Typescript took the Javascript world by storm. If you are a frontend developer, you should check those 15 common Typescript mistakes.
Read more >
The Power of Custom Types - Andrew Scott Chapman - Medium
When you write a program using a strongly typed language, you will declare variables that have a specific data type. These types generally ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found