GithubHelp home page GithubHelp logo

Comments (4)

Eyas avatar Eyas commented on May 11, 2024 3

The error is misleading, but TypeScript is being tripped up by "@context" and excess property checks.

tl;dr: If this is a top-level object, change the type to WithContext<LocalBusiness>. Or otherwise drop @context (you only need a context at the topmost JSON-LD object or graph).

Explanation

TypeScript generally has excess property checks to make sure you're not misspelling something, e.g.:

type A = { x?: string; y?: string; }
const thing: A = {z: string};

while technically thing is a subtype of A, this is often a sign of mistake, so TS will warn that z is wrong.

In schema-dts, @context is only supplied in WithContext<T>. Unfortunately, instead of straightforwardly complaining about @context not being found, TS does it in a very weird way when there's a discriminated union.

from schema-dts.

Eyas avatar Eyas commented on May 11, 2024

I think we can close this, but let me know if you have any issues here!

from schema-dts.

a-ogilvie avatar a-ogilvie commented on May 11, 2024

@Eyas Thank you!! It feels great to have some type safety on my structured data. Thanks to this library, I found a small error in the existing data.

I wonder if the WithContext trick could be documented somewhere. I tried Googling my way out of the problem, but I couldn't find any documentation on this error.

from schema-dts.

Eyas avatar Eyas commented on May 11, 2024

That's a good point! WithContext is briefly mentioned in the "Root Context" section of the readme (right under usage), but if you're troubleshooting your failure (especially cryptic sounding errors from TS) then you'll have trouble finding WithContext as the answer.

Honestly I'm open to feedback. I agree additional documentation is helpful, but I'm not sure what/where. You can always open a PR and fix/add docs as well!

from schema-dts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.