typescript - 打字稿部分<>类型错误
问题描述
我的代码已经工作了,但现在它突然在 logEntry 上有一个类型错误:
Type '{ raw: string; timestamp: number; }' is not assignable to type 'Partial<ILogEntry>'.
Types of property 'constructor' are incompatible.
Type 'Function' is missing the following properties from type 'Model<ILogEntry, {}>': base,
baseModelName, discriminators, modelName, and 59 more.(2322)
实现这一点的正确方法是什么?这是示例代码:
import { Schema, model, Model, Document } from "mongoose";
interface ILogEntry extends Document {
raw: string;
timestamp?: number;
}
const logEntry: Partial<ILogEntry> = {
raw: "hey",
timestamp: 213213,
};
解决方案
Looks like signature of the Document
has been changed, if you don't plan to use constructor
you can simply omit it, if it's acceptable of course.
const logEntry: Partial<Omit<ILogEntry, 'constructor'>> = {
raw: "hey",
timestamp: 213213,
};
and to avoid length to add a type for it
type PartialObj<T> = Partial<Omit<T, 'constructor'>>;
const logEntry: PartialObj<ILogEntry> = {
raw: "hey",
timestamp: 213213,
};
推荐阅读
- php - httaccess 301重定向多个文件
- python - 熊猫字典列到列
- azure - 您正在创建太多 HashedWheelTimer 实例
- azure - 在 Azure DevOps Starter 中更改资源组
- postgresql - 在 Postgres 中存储范围时间序列数据
- regex - NGINX + RegEx:将 sub2.sub1.example.com 重定向到 sub1.example.com
- java - 在java中替换二维数组中的数字?
- mysql - MYSQL 语法 - 如何在查询中使用带条件语句的 where 查询
- docker - 设置 docker 固定 IP 时出错:服务
使用未定义的网络 - java - 如何在java中链接两个列表?