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

Improve TS Type Inference by extending Meteor Check Types #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions easy-schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
import { Match } from 'meteor/check';

export declare const has: unique symbol;

type HasFluentSchema<T> = {
[has]: BaseSchema<T>
};

// Object pattern limited to
export type ObjectPattern<T> = {
type: HasFluentSchema<T>
};

export type Pattern =
| 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.Pattern;

export type PatternMatch<T extends Pattern> =
T extends BaseSchema<infer U> ? U :
T extends HasFluentSchema<infer U> ? U :
T extends [Pattern] ? PatternMatch<T[0]>[] :
T extends Pattern[] ? PatternMatch<T[0]>[] :
T extends ObjectPattern<infer U> ? U :
T extends {[key: string]: Pattern} ? {[K in keyof T]: PatternMatch<T[K]>} :
Match.PatternMatch<T>;

// Base Schema class for common behavior
declare class BaseSchema<T> {
constructor(type: T);
Expand Down Expand Up @@ -87,10 +121,10 @@ export declare const ObjectID: ObjectIDConstructor;
* @param data The data to check
* @param schema The schema to match `data` against
*/
export declare function check<T extends Match.Pattern>(
export declare function check<T extends Pattern>(
data: any,
schema: T
): asserts data is Match.PatternMatch<T>;
): asserts data is PatternMatch<T>;


/** Matches any value. */
Expand All @@ -104,7 +138,7 @@ export declare const Integer: Match.Matcher<number>;
*/
export declare function Optional<T extends Pattern>(
pattern: T
): Matcher<PatternMatch<T> | undefined | null>;
): Match.Matcher<PatternMatch<T> | undefined | null>;

/**
* Shapes an object based on a POJO.
Expand Down Expand Up @@ -155,10 +189,10 @@ export declare const EasySchema: {
};

declare module 'meteor/mongo' {
module Mongo {
namespace Mongo {
interface Collection<T> {
attachSchema(schema: object): void;
schema?: Match.Pattern;
attachSchema<U extends Pattern>(schema: U): Collection<PatternMatch<U>>
schema?: Pattern;
}
}
}
2 changes: 1 addition & 1 deletion lib/attach/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { shape } from '../shape.js';
Mongo.Collection.prototype.attachSchema = function(schema) {
/** @type {import('meteor/check').Match.Pattern} */
this.schema = { ...shape({...schema, ...config.base}), '$id': `/${this._name}` };
return;
return this;
};
2 changes: 1 addition & 1 deletion lib/attach/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Mongo.Collection.prototype.attachSchema = function(schema) {

attachMongoSchema(collection, fullSchema);

return;
return this;
} catch (error) {
console.error(error)
}
Expand Down