microsoft / TypeChat

TypeChat is a library that makes it easy to build natural language interfaces using types.
https://microsoft.github.io/TypeChat/
MIT License
8.06k stars 379 forks source link

TypeChat JSON Validator Incorrectly Rejects Valid Enums #69

Open sho31 opened 11 months ago

sho31 commented 11 months ago

Hello! I included some enums in my schema, but unfortunately the library seems incompatible with schemas containing enums. My JSON data includes values that should correspond to valid enum keys, but the validator seems to incorrectly rejects these values.

Here is a part of the error and the Currency enum : Error: Type '"EUR"' is not assignable to type 'CURRENCY'. {... "currency": "EUR", ...}

export enum CURRENCY { USD = 'USD', EUR = 'EUR', GBP = 'GBP', }

That worked using union type like the following :

type CURRENCY = 'USD' | 'EUR' | 'GBP';

Will enums be supported in the future, or my implementation may be wrong ? Do you think type safety is guaranteed with a union type ?

Thank you !

DanielRosenwasser commented 11 months ago

Do you think type safety is guaranteed with a union type?

Yes. While TypeScript can technically prevent comparisons across different enum types, union types are still type-safe. However, I do see why it's a pain that an enum can't be used with TypeChat.