如何在TypeScript和Meteor.js中设置集合的类型?

huangapple go评论50阅读模式
英文:

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 &quot;meteor/meteor&quot;;
import { Mongo } from &quot;meteor/mongo&quot;;
import { UserType } from &quot;/imports/types/users&quot;;

const Users: Mongo.Collection&lt;UserType&gt; = Meteor.users;

export default Users;

huangapple
  • 本文由 发表于 2023年5月11日 06:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定