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

Add generic properties accessor for complex runtypes #217

Open
yuhr opened this issue Apr 5, 2021 · 2 comments · May be fixed by #218
Open

Add generic properties accessor for complex runtypes #217

yuhr opened this issue Apr 5, 2021 · 2 comments · May be fixed by #218

Comments

@yuhr
Copy link
Collaborator

yuhr commented Apr 5, 2021

I've come up with this idea during investigating #216. Currently, accessing inner runtypes of complex ones such as Record, Tuple, etc. in a generic way requires you to write cumbersome code like following:

runtype.reflect.tag === 'record' ? runtype.reflect.fields : runtype.reflect.tag === 'tuple' ? runtype.reflect.components : ...

But I want to write just this instead:

runtype.properties

This should be possible because JS and TS maintain interoperability between objects and arrays, i.e. arrays can be treated as objects with keys being string of decimal numbers.

If implemented properly, this should also work with Union and Intersect, because TS already does some equivalent stuff for static typing, for example:

type U = { foo: true } | { foo: false };
let u!: U;
u.foo; // inferred as `boolean`

type I = { foo: boolean } & { foo: boolean; bar: string };
let i!: I;
i.foo; // inferred as `boolean`
i.bar; // inferred as `string`

So you should be able to do that on runtypes:

const U = Union(Record({ foo: Literal(true) }), Record({ foo: Literal(false) }));
u.properties.foo; // should be `Union(Literal(true), Literal(false))` or just `Boolean`

const I = Intersect(Record({ foo: Boolean }), Record({ foo: Boolean, bar: String }));
i.properties.foo; // should be `Intersect(Boolean, Boolean)` or just `Boolean`
i.properties.bar; // should be `String`
@auchtopus
Copy link

This seems pretty useful! Is there still any development required for it?

@yuhr
Copy link
Collaborator Author

yuhr commented Dec 11, 2023

I'm now working on a large refactoring (#339 and the like) of the entire codebase as it's been so hard to maintain due to indirect/tortuous logic rendered by historical reasons. After I've completed it, I'll take this on.

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

Successfully merging a pull request may close this issue.

2 participants