Giter VIP home page Giter VIP logo

Comments (2)

sinclairzx81 avatar sinclairzx81 commented on July 20, 2024

@s-egea Hi! Sorry for the delay.

I wonder if it would make sense to add support for using Transform types in Record keys (not just at the type level) ?

Hmmm, I'm not too sure about this. There isn't really an mechanism that would enable Record keys to be transformed (either for Encode/Decode), and the best next thing I can think of would be to discard the Transform when applied to keys (which may not be expected).

Currently, the Record type only accepts a few known key types (string, number, union literal, template literal, generally any type that can naturally encode the type as part of the patternProperties sub schematic). Given that Transforms augment the schematic in ways that cannot encode to patterns, it generally falls outside a supported type (even if the type happens to be String). I think on this basis, I think it's correct to deem transforms as non-viable record keys which should reflect in the inference as TNever

const K = T.Transform(T.String())
  .Decode((v) => v)
  .Encode((v) => v);

const S = T.Record(K, T.String()); // S is T.TNever

Will close out this issue for now as I don't think there is much that can be done to resolve in a satisfactory way that makes sense for Transforms (as they currently are). But I'll drop a suggestion tag on this issue and revisit in future. There are planned updates to Transforms (and validation) in later releases, and there may be potential to explore this under a updated validation infrastructure.

Cheers!
S

from typebox.

s-egea avatar s-egea commented on July 20, 2024

No worries about the delay!

Iā€™m just dropping some ideas on how I thought it could be implemented. I apologize in advance if my understanding is incorrect or incomplete.

As I find it difficult to explain my ideas using text only, I will share two drawings instead:

  • The first drawing describes how I understand standard and Transform types.
  • The second drawing describes the ideas about how Transform types could be used as keys in Record.

TB_types

transform_keys

I'm aware that this use case is not very common, so Iā€™m perfectly fine continuing with my (type) patch for now.

There are planned updates to Transforms (and validation) in later releases, and there may be potential to explore this under an updated validation infrastructure.

Note that my current project makes extensive use of Transform types, so I will be happy to share some feedback and pain points I encountered.

Thanks again!

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.