Typescript Mongo Schema-类型“ save”的参数不能分配给类型“ never”的参数

我正在使用接口创建mongo模式,但是由于某种原因,它找不到保存方法。保存功能不应该与mongo文档模块分开吗?我在这里做什么错了?

s.ts

import { Document } from 'mongoose';

export interface IS extends Document{
    name: string,}

sSchema.ts

import mongoose,{ Schema } from "mongoose";
import { IS } from "../interfaces/s/s";

export var SSchema: Schema = new Schema({
    name: { type: String,required: true,unique: true }
})

// Export the model and return your S interface
export default mongoose.model<IS>('S',SSchema);

sTest.ts

import mongoose from 'mongoose';
const { MongoClient } = require('mongodb');
import S from "../schemas/s"
import { IS } from "../interfaces/s/s";

describe('S model',() => {
it('Should save a s',async () => {

 var s: IS = new S({
            name: 'Test first name'
        });

        const spy = jest.spyOn(s,'save');
        s.save();

})
})

它抛出2个错误:

TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
__tests__/sTest.ts:47:41 - error TS2345: Argument of type '"save"' is not assignable to parameter of type 'never'.

47         const spy = jest.spyOn(s,'save');
                                     ~~~~~~
__tests__/sTest.ts:48:17 - error TS2339: Property 'save' does not exist on type 'IS'.

48         s.save();
             ~~~~
shanyecai 回答:Typescript Mongo Schema-类型“ save”的参数不能分配给类型“ never”的参数

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3119989.html

大家都在问