Comments (2)
@aleclarson Hi!
Ah, this is interesting :) TypeBox doesn't actually have an internal concept of Enum
. Instead, Enum is internally represented as Union<Literal[]>
(which is the generic form). The reasoning behind this is to mitigate multiple representations of things that are Union-like. From this, TypeBox uses the general form to simplify computations relating to Unions (where it's simpler to focus on one union representation than having to map from potentially multiple representations)
import { Type, TypeGuard } from '@sinclair/typebox'
enum Foo { A, B }
const T = Type.Enum(Foo) // {
// anyOf: [
// { const: 0, type: 'number', [Symbol(TypeBox.Kind)]: 'Literal' },
// { const: 1, type: 'number', [Symbol(TypeBox.Kind)]: 'Literal' }
// ],
// [Symbol(TypeBox.Hint)]: 'Enum',
// [Symbol(TypeBox.Kind)]: 'Union'
// }
const R = TypeGuard.IsUnionLiteral(T) // true - stand in for Enum representation
There are considerations to changing this in future (as enum
schema representations are often asked for by OpenAPI users), but there needs to be work done to finalize how TypeBox evaluates both Intersection / Union (which has been a long standing technical challenge given the need to compute both static and runtime type, and there is a lot of nuance to replicating distributive types). Once this work has been done though, TB would be ready to adopt multiple representations for union (where Enum can remap into Union for composition elsewhere), and the above representation would look as follows.
import { Type, TypeGuard } from '@sinclair/typebox'
enum Foo { A, B }
const T = Type.Enum(Foo) // { enum: [0, 1]: [Symbol(TypeBox.Kind)]: 'Enum' }
const R = TypeGuard.IsEnum(T) // true - as we have a actual enum representation
For now though, the TypeGuard.IsUnionLiteral() is the best approximation for Enum.
Hope this brings some insights!
S
from typebox.
@aleclarson Heya,
Might close off this one as IsEnum is currently out of scope until TB supports a enum
type representation.
Cheers!
S
from typebox.
Related Issues (20)
- 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>` HOT 7
- Error when using Composite with Ref (Unable to dereference schema with $id 'undefined') HOT 3
- Transform type keys in Record HOT 2
- Is it possible to add support to the Type.String additional properties for the enum keyword HOT 4
- Support for async validations? HOT 1
- Transform + Map/Index does not work HOT 4
- Update the error message for required properties HOT 3
- Covariance issue with `TypeCheck<TSchema>` and `TypeCheck<TObject<{}>>` HOT 1
- Default values in nested objects are not generated HOT 2
- Doubt on Record HOT 2
- TypeCompiler compile on Type.Strict(T) - Preflight validation check failed to guard for the given schema HOT 2
- Max call stack size exceeded HOT 1
- Value.Convert() doesn't work with nullable record HOT 1
- Variadic Any Typed function parameters HOT 2
- How to do a cross field validation? HOT 3
- Undefined is incorrectly handled in Diff HOT 3
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.