Collection2: How to add proper typings for "index"?

If I turn a SimpleSchema schema definition into TypeScript, I get an type error when using the “index” option. How can I fix this?

Could you post some code sample here? what’s problem?

If I have a schema declaration like this:

the index is highlighted as an error.

Maybe this is because I am still using simpl-schema and not the revised meteor/aldeed:simple-schema?

4.0

Starting 4.0 collection2 ships with built in aldeed:simple-schema@1.13.1.

meteor add aldeed:collection2

I don’t know your setup but if you use aldeed:collection2 package, it doesn’t even support typed.
There is no type definition here: meteor-collection2/package/collection2 at master · Meteor-Community-Packages/meteor-collection2 · GitHub
or here: meteor-simple-schema/package.js at master · Meteor-Community-Packages/meteor-simple-schema · GitHub

To fix the TypeScript error with the index option in your SimpleSchema definition, you can define a custom type for the index.

typescript

import { SimpleSchema } from 'simpl-schema';

const MySchema = new SimpleSchema({
  myField: {
    type: String,
    index: true, // This can cause a type error
  },
});

// Cast the schema to have the correct typings
const typedSchema = MySchema as unknown as SimpleSchema & { index?: boolean };

export default typedSchema;

This way, TypeScript will recognize the index property without throwing an error.