Typescript - replace SimpleSchema or use alongside it?

Pretty much the question on the title. There is this old thread from 3 years back - I wasn’t sure whether it would be a good idea to bump that seeing as official TS support has only just come to the fore.

The Meteor TS boilerplate project defines Mongo Collections using just a TS interface - which makes sense in terms of simplicity. However, SimpleSchema provides more than just static typing (most notably the runtime data manipulations/protections), so I would think that using them both would be better, albeit more effort?

Does anyone have any thoughts one way or the other?

Not much experience with TS, yet, as we are still in the consideration phase.

For us, we are using simpleSchema to protect our app from wrong inputs coming from the client. This is something that TS will not be able to provide. We also use simpleSchema in a number of custom validations depending on the inputs

I see simpleSchema as a runtime tool (as you’ve also mentioned).

1 Like

Agree on all points.

I’m just in the process of combining the two. Though I think it will require some type assertions or similar as I don’t think Simple Schema has the typings necessary. E.g. Getting this TS warning:

Property 'schema' does not exist on type 'Collection<MyDocumentType>'

What you need is typings for the collection2 meteor package - you can base them on some old work at https://github.com/meteor-typescript/meteor-typescript-libs/blob/master/definitions/collection2.d.ts

So what you can do is simply to augment the existing Mongo.Collection interface. The contents should look something like this (this is what I use):

declare module "meteor/mongo" {
  import { SimpleSchema } from "simpl-schema";
  namespace Mongo {
    interface Collection<T> {
      /**
       * collection2 extension
       */
      attachSchema(schema: SimpleSchema): void;
    }
  }
}

You can put this snippet into a .d.ts file or just anywhere in your existing source code.

3 Likes

That’s awesome, thanks for the guidance!

Added one property to come up with:

declare module "meteor/mongo" {
  namespace Mongo {
    interface Collection<T> {
      attachSchema(schema: SimpleSchema): void;
      schema: SimpleSchema;
    }
  }
}

I also had to use one assertion

Collection.schema = new SimpleSchema({
  prop: value,
  prop2: value2
} as { [key: string]: any; })

Probably not the most detailed typings but it will do for now

1 Like

I need this Guide too!

+1 for upgrading SimpleSchema with Typescript, JsonSchema, and AJV.

2 Likes

Anyone know why a …d.ts file may NOT be getting read/compiles (not sure of the right word here).

I tried moving out the above code to its own meteorDefs.d.ts file in the project root (to keep it separate from my custom types) and it does not seem to register:

// File location: root/meteorDefs.d.ts

import SimpleSchema from 'simpl-schema';

declare module "meteor/mdg:validated-method" {
  // TODO
}

declare module "meteor/mongo" {
  namespace Mongo {
    interface Collection<T> {
      attachSchema(schema: SimpleSchema): void;
      schema: SimpleSchema;
    }
  }
}