Skip to content

Conversation

@ccharly
Copy link
Contributor

@ccharly ccharly commented Jan 28, 2025

Currently, all our CAIP structs are being inferred as string. This PR adds a new definePattern which allows to using a template literal string instead.

Also, this new definePattern allows to "name" the pattern. Here's an example:

const HexStringPattern = pattern(string(), hexPattern);
const HexString = definePattern('HexString', hexPattern);

assert('foobar', HexStringPattern);
// StructError: Expected a string matching `/^0x[0-9a-f]+$/` but received "foobar"

assert('foobar', HexString);
// StructError: Expected a value of type `HexString`, but received: `"foobar"`

If you think the new definePattern should go in a separate PR, I can split that up.

I do believe this is BREAKING CHANGE since the error messages will be different, so we can expect the consumers of this packages to update some of there tests (but I don't really know if we consider this a breaking change in other packages?)

@ccharly ccharly force-pushed the feat/named-caip-structs branch from c6f90b3 to 3518871 Compare January 28, 2025 16:25
@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

export const CaipChainIdStruct = definePattern<`${string}:${string}`>(
'CaipChainId',
CAIP_CHAIN_ID_REGEX,
) as Struct<CaipChainId, null>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the PR description you say that the struct is being inferred as a string, but it looks like we were using a type assertion before to ensure that this was not the case. So, was it really being inferred as a string? It's not clear to me what effects these changes have, as the only tests being added in this PR are for definePattern and not for any of the structs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pattern(string(), ...) would result in a type like this Struct<string, ...>

Meaning that using Infer<typeof MyType> would be inferred as string.

With the new definePattern helper I've added, we now uses a generic type to inject it into the Struct like so:

export const CaipChainIdStruct = pattern(
  string(),
  CAIP_CHAIN_ID_REGEX,
); // Would be: Struct<string, null> (which is why we were type-casting)

export const CaipChainIdStruct = definePattern<`${string}:${string}`>(
  'CaipChainId',
  CAIP_CHAIN_ID_REGEX,
); // Would be: Struct<`${string}:${string}`, null> (no more type-casting required)

In addition to that, we use define under the hood, which allows use to have a "named" struct (and better error messages).

@ccharly ccharly force-pushed the feat/named-caip-structs branch from 87c6bb0 to a920704 Compare January 29, 2025 09:27
Co-authored-by: Daniel Rocha <68558152+danroc@users.noreply.github.com>
danroc
danroc previously approved these changes Jan 29, 2025
@ccharly ccharly marked this pull request as draft January 29, 2025 09:42
Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
@ccharly ccharly dismissed stale reviews from FrederikBolding and danroc via 9eac420 January 29, 2025 09:58
@ccharly ccharly marked this pull request as ready for review January 29, 2025 10:01
@FrederikBolding
Copy link
Member

feat! -> feat? 😄

@ccharly ccharly changed the title feat!: named CAIP structs feat: named CAIP structs Jan 29, 2025
@ccharly ccharly merged commit 9ac7ec2 into main Jan 29, 2025
18 checks passed
@ccharly ccharly deleted the feat/named-caip-structs branch January 29, 2025 10:12
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

Successfully merging this pull request may close these issues.

6 participants