Git Product home page Git Product logo

Comments (5)

luin avatar luin commented on August 16, 2024

Hey @alecgibson 👋,

Thanks for raising this up! I think in practice, you would get delta data from your database and you still have to perform a force casting (Delta<unknown> as Delta<MyOp>) anyway so I don't think it worth the effort to make it generic.

from delta.

alecgibson avatar alecgibson commented on August 16, 2024

My point was more that you can simplify your casting by moving the cast to the Delta level, so you cast just once (on data retrieval), and then after that you're working with a well-typed object, and don't have to cast every time you access an attribute.

from delta.

luin avatar luin commented on August 16, 2024

While this is indeed convenient, the actual data you get may not necessarily conform to your type definition (e.g. due to bugs or data format upgrades), and supporting generics can easily lead developers to overlook this issue. In practice, I'd recommended using typeof attributes.header === 'number' to narrow down the type, although as you said, it is more verbose. Another issue I'm considering is the support for the embed format (op.insert.image). If we provide generics for Op, then it would make no sense not to provide generics for the embed format as well.

from delta.

alecgibson avatar alecgibson commented on August 16, 2024

I'll admit embeds do complicate things, and yes I'd agree we'd want generics for that, too if we did attributes.

If there's no appetite for this, that's fair enough. Thought I'd just float the idea because I know I get bored of constantly casting, but maybe I'm just lazier than most 😛

from delta.

luin avatar luin commented on August 16, 2024

For me I tend to use type guards for attributes and type force casts for embed values as attributes are generally simple values. Probably you may want to define a custom utility function getAttributes(op: Op): MyAttributes to avoid casting.

from delta.

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.