首页 > 解决方案 > Meteor Mongo Collection 未使用 typescript 接口

问题描述

我想通过 typescript 接口描述 Mongo.Collection 模式,以严格检查 fetch、forEach 等的类型。

interface IChat {
  _id: string;
  name?: string
};

const Chats = new Mongo.Collection<IChat>('chat');

// (method) Mongo.Cursor<IChat>.forEach(callback: <T>(doc: T, index: number, cursor: Mongo.Cursor<T>) => void, thisArg?: any): void
Chats.find().forEach(c => {
  console.log(c._id); // Property '_id' does not exist on type 'T'.
  // Why "type T" if it should be the type IChat???
});

但面临下一个错误:Property '_id' does not exist on type 'T'.

我做错了什么?

链接到打字稿游乐场

错误

Collection.find().forEach() 定义

标签: mongodbtypescriptmeteorcollectionsinterface

解决方案


推荐阅读