Coder Social home page Coder Social logo

Comments (6)

sinclairzx81 avatar sinclairzx81 commented on May 23, 2024 2

Hey, alright cool. Symbol as a key it is :) Will also rename to modifierSymbol as per suggestion and push to npm later this evening.

It's a nice update this one. Thanks again for the suggestion!

Cheers
S

from typebox.

sinclairzx81 avatar sinclairzx81 commented on May 23, 2024

Hey, that's a great idea :)

I have just pushed an update into the feature/symbol-modifier branch that takes the Symbol approach you suggested. You're right that using a Symbol omits the property from the resulting JSON.stringify which is certainly desirable (leaving a clean standard schema when serialised).

Ill leave this in the branch for you to have a quick review of the updates. If you're happy there (or have amendments you would like to see) happy to review those, otherwise, can publish these changes through later today.

Thanks for the suggestion !

from typebox.

geekflyer avatar geekflyer commented on May 23, 2024

Heyho, thanks for the quick response. I looked at your branch and I guess this could work but what I actually meant is not to use a symbol for the modifier value, but a symbol for the modifier property key.

So for example instead of:

  /** Modifies the inner type T into an optional T. */
  public static Optional<T extends TSchema | TUnion | TIntersect>(item: T): TOptional<T> {
    return { ...item, modifier: 'optional' }
  }

you could:

const ModifierSymbol = Symbol('TypeBoxModifier');

  /** Modifies the inner type T into an optional T. */
  public static Optional<T extends TSchema | TUnion | TIntersect>(item: T): TOptional<T> {
    return { ...item, [ModifierSymbol]: 'optional' }
  }

This would hide the property also from enumeration on the plain JavaScript object, not only from the serialized JSON.

If you run into problems with this approach (I vaguely remember TypeScript has in some edge-cases problems with typing symbol property names, but not quite sure if that's still the case), you could also just define non enumerable property, such as:

const ModifierKey = '__TypeBoxModifier';

  /** Modifies the inner type T into an optional T. */
  public static Optional<T extends TSchema | TUnion | TIntersect>(item: T): TOptional<T> {
    const optionalizedSchema = { ...item };
    Object.defineProperty(optionalizedSchema, '__TypeBoxModifier', {
      enumerable: false,
      value: 'optional'
    }
    return optionalizedSchema; 
  }

In both cases it's probably also a good idea to export the ModifierSymbol or ModifierKey so that users of typebox can access the modifier if they really want to (may be useful for some custom stuff etc.).

from typebox.

sinclairzx81 avatar sinclairzx81 commented on May 23, 2024

Hey, thanks for the follow up.

I checked in a an extra branch feature/symbol-modifier-key that uses the Symbol as a Key as per suggestion. I do kinda like that the modifier property remains enumerable in some capacity (I feel this is probably less surprising for the end user), but given the nature of the thing, I'm open to it being a non-enumerable key as well.

If you feel the modifier is best expressed as a symbol-key, can roll that branch through instead. As before, the implementation is there for quick review, can publish it through later tonight if all looks good.

Cheers
S

from typebox.

geekflyer avatar geekflyer commented on May 23, 2024

Ok nice. Checked out the symbol as key branch it looks good to me! Just had a minor comment on the symbol variable name.

Overall I kinda prefer the symbol as key pattern because that's what I think is relatively common - I have seen this particular pattern of using symbols in a couple of other libraries and some of the intros to symbols mention that as well.
The fact that keys which have a symbol value (instead of symbol key) get elided in JSON.stringify() was actually kinda surprising to me, so personally that's another reason I prefer the subjectively lesser "magic" of the symbol as key solution.

Thanks again for the quick response!
Cheers Chris

from typebox.

sinclairzx81 avatar sinclairzx81 commented on May 23, 2024

Updates published on @sinclair/[email protected] :)

Cheers !

from typebox.

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.