Comments (2)
@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.
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.
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)
- Should `IsValueType` guard consider `Date`? HOT 1
- ESNext target HOT 3
- Trying to do codegen from Drizzle schemas HOT 1
- Make `default` and `examples` schema options properties type-safe HOT 2
- Indicate which files don't have "sideEffects" in package.json for improved tree shaking HOT 3
- Type.String().Optional() is accepted on ts-hint / ts-lint, and even build successfully; but of course, "Optional()" does not exists HOT 2
- Dynamic Template Literals can't be Mapped HOT 2
- How should TypeBox types be used with TS type predicates? HOT 2
- TypeCompiler.Compile incorrectly parsing `Record<any, any>`
- Error when using Composite with Ref (Unable to dereference schema with $id 'undefined') HOT 3
- Is it possible to add support to the Type.String additional properties for the enum keyword HOT 2
- Support for async validations? HOT 1
- Transform + Map/Index does not work HOT 4
- Update the error message for required properties
- Covariance issue with `TypeCheck<TSchema>` and `TypeCheck<TObject<{}>>`
- Default values in nested objects are not generated HOT 1
- Doubt on Record HOT 2
- TypeCompiler compile on Type.Strict(T) - Preflight validation check failed to guard for the given schema
- Max call stack size exceeded
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.