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

patterns: Constructing a pattern should also create a static type #2392

Open
gibson042 opened this issue Jul 31, 2024 · 2 comments
Open

patterns: Constructing a pattern should also create a static type #2392

gibson042 opened this issue Jul 31, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@gibson042
Copy link
Contributor

gibson042 commented Jul 31, 2024

What is the Problem Being Solved?

Independently constructing runtime patterns with M matcher methods and defining static types with @typedef is cumbersome and error-prone, leading us to implement manual validation even when M would be useful (cf. Agoric/agoric-sdk#9169 (comment)). It should instead be possible to extract static types from patterns, e.g.

import { mustMatch } from '@endo/patterns';

const BundleShape = M.or();

/**
 * @param {TypeFromPattern<typeof BundleShape>} bundle
 */
const evaluateBundle = bundle => {
  mustMatch(bundle, BundleShape);
  const { moduleFormat } = bundle;
  
};

evaluateBundle({}); // static error

Description of the Design

Annotate Matcher functions to return annotated values, and define a utility type for reading such annotations into types.

Security Considerations

None AFAIK

Scaling Considerations

None AFAIK

Test Plan

We should take examples from existing endo and agoric-sdk code.

Compatibility Considerations

None AFAIK

Upgrade Considerations

Nothing out of the ordinary, just the usual caveat that functionality is not incorporated into mainnet vats until they are upgraded.

@gibson042 gibson042 added the enhancement New feature or request label Jul 31, 2024
@gibson042
Copy link
Contributor Author

gibson042 commented Jul 31, 2024

A successful experiment that leverages const annotation for raw primitives: TypeScript Playground

I'm near the current limits of my knowledge, but this looks very promising as a step beyond even https://github.com/Agoric/agoric-sdk/blob/master/packages/internal/src/types.d.ts#L75-L96 .

screen

@gibson042
Copy link
Contributor Author

gibson042 commented Aug 2, 2024

And a version with recursive types (which must be actual TypeScript AFAIK) for recognizing e.g. { version: /** @type {const} */ (1), contents: M.string() } as a Pattern without the need for a wrapping call: TypeScript Playground

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

No branches or pull requests

1 participant