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

Thoughts on extending createAssertType() #131

Open
dko-slapdash opened this issue Jul 11, 2022 · 1 comment
Open

Thoughts on extending createAssertType() #131

dko-slapdash opened this issue Jul 11, 2022 · 1 comment

Comments

@dko-slapdash
Copy link
Contributor

dko-slapdash commented Jul 11, 2022

Hi.

@woutervh- Are you okay if I submit a PR to add the following feature to the value returned by createAssertType() - see example below:

export interface MyType {
  prop: string;
}

export const MyType$ = createAssertType<MyType>();

... // below are the illustrations:

const asserted = MyType$(req.params); // throws if no match

const maybeAsserted = MyType$.to(req.params); // doesn't throw; returns MyType | undefined

const prop = MyType$.is(req.params) ? req.params.prop : "no"; // assertion guard

Motivation: I want to have a single entry point to the assertions on some type MyType with some opinionated name (like MyType$), and then, having this entry point, be able to access it in 3 different modes (throwing, optional, guard).

Another motivation for me is to move all the typescript-is related code to a separate monorepo module, then export all those $-assseters to be usable by other modules, and then keep only that independent module built with tsc+plugins (and have the rest of modules built by swc which doesn't support TS plugins).

P.S.
These is actually an even cooler syntax! No need for $, the type name and the const name can be identical:

export interface MyType {
  prop: string;
}

export const MyType = createAssertType<MyType>();
@dko-slapdash
Copy link
Contributor Author

@woutervh- What do you think?

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

1 participant