英文:
How to set the type of the collection in typescript and meteor.js?
问题
我真的很新手 TypeScript,想知道如何给集合设置类型。
这是我的 React 组件的一部分。我需要从 Users 集合中获取用户文档。
const user = useTracker(() => Users.find({_id: userId}).fetch(), [userId]);
在我的代码编辑器中,我遇到了 `Cannot find name 'Users'. Did you mean 'user'?` 错误。
我并不是想要输入 `user`,集合的名称是 `Users`。
问题在于 Users 集合文件是用 `js` 而不是 `tsx` 写的。
有办法定义 Users 集合的类型吗?
英文:
I’m really new to typescript and wonder how to set a type to a collection.
This is my part of the react component. I need to pull a user document from Users 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 to type user
, the collection name is the 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?
答案1
得分: 3
我使用这种方法:
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;
英文:
I use this method:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论