How to set the typf of the collection in typescript?

I’m really new to typescript and wonder how to set a type to a collection.

  const user = useTracker(() =>Users.find({_id: userId}).fetch(), [
    userId,
  ]);

I have a Cannot find name 'Users'. Did you mean 'user'? error on my code editor.
I didn’t not mean user, the collection name is Users.

The thing is the users collection file is written in js not tsx.

Is there a way to define a type of the Users collection?

I use this on my project.

import { Meteor } from "meteor/meteor";
import { Mongo } from "meteor/mongo";
import { UserType } from "/imports/types/users";

const Users: Mongo.Collection<UserType> = Meteor.users;

export default Users;

Regarding the file extensions, I use .ts for everything except React component/hook which is .tsx

1 Like

‘users’ is predefined collection in Meteor. To access it you just to use Meteor.users.
If you have your own collections then you could do something like this

export interface Department {
  _id: string,
  name: string,
  description?: string,
}

export const Departments = new Mongo.Collection<Department>('departments');

Read docs here - Collections | Meteor API Docs

1 Like

Yes, for example, we do it this way:

export type User = Meteor.User & {
  _id?: string,
  email?: string,
  profile?: {
    fullname: string,
    telegramId?: number,
    // ...
  }
};

export const useUsers = (
  selector?: Mongo.Selector<User>
): [ready: boolean, users: User[]] => useTracker(() => {
  const ready = Meteor.subscribe('users', selector).ready();
  const users = Meteor.users.find(selector).fetch() as User[];
  return [ready, users];
}, [JSON.stringify(selector)]);
1 Like

It looks like @minhna 's answer worked the best for me.
Thank you both for the help! @bladerunner2020

2 Likes