Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeGuard.IsEnum #811

Closed
aleclarson opened this issue Mar 25, 2024 · 2 comments
Closed

TypeGuard.IsEnum #811

aleclarson opened this issue Mar 25, 2024 · 2 comments

Comments

@aleclarson
Copy link

This should exist :)

@sinclairzx81
Copy link
Owner

@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

@sinclairzx81
Copy link
Owner

@aleclarson Heya,

Might close off this one as IsEnum is currently out of scope until TB supports a enum type representation.

Cheers!
S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants