You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been playing around with jam:easy-schema and jam:methods in a project installed as a git submodule in the packages folder instead of the traditional way which let me play around a bit with the easy-schema.d.ts file.
I noticed some potential improvements to type inference could be made, but I'm not sure how much this affects performance or has any limitations for other users. I had to do a lot of reloading in the editor to see the improvements each time, which takes "the usual 2 seconds or so" for TS to start linting.
Anyway, I need to clean up the changes in a new branch, match the code style etc, and make a pull request... but the summary is basically as follows.
1. Extend the types provided by Meteor Check:
// easy-schema.d.tsimport{Match}from'meteor/check'exportdeclareconsthas: unique symbol;typeHasFluentSchema<T>={[has]: BaseSchema<T>}// Object pattern limited to exporttypeObjectPattern<T>={type: HasFluentSchema<T>}exporttypePattern=|BaseSchema<any>|HasFluentSchema<any>|ObjectPattern<any>|[Pattern]// Meteor Check doesn't require Pattern[], because check schemas are ephemeral// so typescript gives e.g. [String] the strict type of [StringConstructor]// but a schema declared as a variable does not have this benefit of the doubt// and is inferred as StringConstructor[]// Object.freeze or `as const` makes TS complain about mutability... can't win.|Pattern[]|{[key: string]: Pattern}|Match.PatternexporttypePatternMatch<TextendsPattern>=TextendsBaseSchema<infer U> ? U :
TextendsHasFluentSchema<infer U> ? U :
Textends[Pattern] ? PatternMatch<T[0]>[] :
TextendsPattern[] ? PatternMatch<T[0]>[] :
TextendsObjectPattern<infer U> ? U :
Textends{[key: string]: Pattern} ? {[KinkeyofT]: PatternMatch<T[K]>} :
Match.PatternMatch<T>
2. Everything that uses Match.Matcher needs its signature updated
The gist is that Match.Matcher stays, but Match.Pattern or Match.PatternMatch need to be replaced with the local version instead.
Actually I found an issue with the types in main's HEAD, they were not namespaced properly in some cases so inference was broken originally because of that, but I really wanted to get all the extra schema definition patterns working too so I've gone far past that point
- export declare function Optional<T extends Match.Pattern>(+ export declare function Optional<T extends Pattern>(
pattern: T
- ): Match.Matcher<Match.PatternMatch<T> | undefined | null>;+ ): Match.Matcher<PatternMatch<T> | undefined | null>;
3. Modify attachSchema to return its own instance, and add assertion return type
declare module 'meteor/mongo'{
module Mongo{// If you're brave you can modify the signature so it accepts an additional type param// representing the schema's pattern, but seems pretty messy// also this might affect the `transform` method's type safety.interfaceCollection<T>{attachSchema<UextendsPattern>(schema: U): Collection<PatternMatch<U>>// Also make sure this points to the right patternschema?: Pattern;}}}
// TS will complain about `name` and `dob`constv=awaitcoll.findOneAsync({name: 10,dob: 'hi'})// v's inferred type is:constv: {_id: string;name: string;age: number|null|undefined;testing: string;uri: string;codes: string[];dob: Date[];}|undefined
Let me know how this sounds, I won't have time to prepare a PR straight away so I can come back and do that. But maybe I'm working in the wrong direction to what you want for the library, e.g. if you wanted to add Zod support or something which might make this a little redundant.
The text was updated successfully, but these errors were encountered:
Hey, sounds great. I'm all for improvements that make the DX better. I wouldn't expect any performance impacts since the changes seem isolated to the .d.ts but let me know if I'm missing something.
I don't plan to support Zod with this package so I'm sure other TS users will welcome your suggested changes.
Hi there,
I've been playing around with jam:easy-schema and jam:methods in a project installed as a git submodule in the
packages
folder instead of the traditional way which let me play around a bit with theeasy-schema.d.ts
file.I noticed some potential improvements to type inference could be made, but I'm not sure how much this affects performance or has any limitations for other users. I had to do a lot of reloading in the editor to see the improvements each time, which takes "the usual 2 seconds or so" for TS to start linting.
Anyway, I need to clean up the changes in a new branch, match the code style etc, and make a pull request... but the summary is basically as follows.
1. Extend the types provided by Meteor Check:
2. Everything that uses Match.Matcher needs its signature updated
The gist is that Match.Matcher stays, but Match.Pattern or Match.PatternMatch need to be replaced with the local version instead.
Actually I found an issue with the types in main's HEAD, they were not namespaced properly in some cases so inference was broken originally because of that, but I really wanted to get all the extra schema definition patterns working too so I've gone far past that point
3. Modify attachSchema to return its own instance, and add assertion return type
Then you can do e.g.:
And you should get something inferred like:
Let me know how this sounds, I won't have time to prepare a PR straight away so I can come back and do that. But maybe I'm working in the wrong direction to what you want for the library, e.g. if you wanted to add Zod support or something which might make this a little redundant.
The text was updated successfully, but these errors were encountered: