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

Support for intrinsic string manipulation on TString #690

Open
quentinverlhac opened this issue Dec 11, 2023 · 2 comments
Open

Support for intrinsic string manipulation on TString #690

quentinverlhac opened this issue Dec 11, 2023 · 2 comments

Comments

@quentinverlhac
Copy link

Hello,

This issue is reporting what seems to me an inconsistency with TypeScript behaviour.

Since this pull request, TypeScript allows a type to represent all the strings that are Uppercase, Lowercase, Capitalize, or Uncapitalize.
These types are Uppercase<string>, Lowercase<string> Capitalize<string> and Uncapitalize<string> respectively.

However it seems that typebox doesn't support this feature.
Applying an intrinsic type on a string type gives a TString type, which is then inferred into string type instead of following the TypeScript behaviour.

For example:

const LowercaseStringSchema = Type.Lowercase(Type.String()) // LowercaseStringSchema is TString

type LowercaseStringType = Static<typeof LowercaseStringSchema> // LowercaseStringType is string

But in TypeScript:

type LowercaseString = Lowercase<string> // LowercaseString is Lowercase<string> instead of string

Would it be possible to support this feature when inferring the static type of a schema?
Maybe there is a good reason not to support this feature and I missed it, apologies if this is the case.

Thank you for supporting this package!

@sinclairzx81
Copy link
Owner

sinclairzx81 commented Dec 12, 2023

@quentinverlhac Hi,

Thanks for the suggestion, I think this is a good idea. Currently intrinsic string manipulation types are only supported for remapping finite literal/template literal values, with the intrinsic applying an immediate remap of the type.

const T = Type.Uppercase(Type.Literal('hello')) // T: TLiteral<'HELLO'>

Where above the return value is evaluated immediately to be { const: 'HELLO' } (which is meaningful to Json Schema). However I can imagine scenarios like the following.

const T = Type.Object({
  value: Type.Uppercase(Type.String()) // only allow strings that are uppercase
})

In which case the schema would be { type: 'string', pattern: '^[A-Z]*$' }, and where TypeBox would need to represent that to the type system as TUppercase<T> such that it can be correctly composed with other types, such as TemplateLiteral.

I will do a review of what would be required to make it work once 0.32.0 is published. Unfortunately, I don't expect this functionality to land soon (as I expect implementing this correctly would involve updating a quite a few existing types to ensure they map correctly), but will try pick it up early to mid next year.

Thanks for the suggestion!
S

@quentinverlhac
Copy link
Author

quentinverlhac commented Dec 12, 2023

Hi @sinclairzx81,
Thanks for considering this suggestion. Using regex in the JSON schema would be very useful as well.
I'm looking forward to seeing it implemented!

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

No branches or pull requests

2 participants