typescript-is
typescript-is copied to clipboard
Thoughts on extending createAssertType()
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>();
@woutervh- What do you think?