TypeScript - “{}”类型的参数不可分配给“{}”类型的参数 - MongoStore
TypeScript - Argument of type '{}' is not assignable to parameter of type '{}' - MongoStore
我对 TS 比较陌生,在 new MongoStore()
参数中遇到了类型问题。通常我可以通过在 JS 中写入 db: database
作为参数来重用 Native MongoDB 连接,但是 TS 显示以下错误:
Argument of type '{ db: Db; }' is not assignable to parameter of type 'MongoUrlOptions | MogooseConnectionOptions | NativeMongoOptions | NativeMongoPromiseOptions'.
Type '{ db: Db; }' is not assignable to type 'NativeMongoPromiseOptions'.
Property 'dbPromise' is missing in type '{ db: Db; }'.
我试过写 new MongoStore({ db: database } as NativeMongoOptions)
,但这也无济于事。
[更新] 添加 as NativeMongoOptions
会产生差异错误:
Type '{ db: Db; }' is not assignable to type 'NativeMongoOptions'.
Types of property 'db' are incompatible.
Type 'import("/node_modules/@types/mongodb/index").Db' is not assignable to type 'import("/node_modules/@types/connect-mongo/node_modul...'.
Property 'authenticate' is missing in type 'Db'.
这是我的代码问题还是 TS 类型问题?
下面是我的代码片段。
const MongoStore: connectMongo.MongoStoreFactory = connectMongo(session);
const app: express.Application = express();
enableMiddleware(app);
(async () => {
try {
const client: mongodb.MongoClient = await mongodb.MongoClient.connect(config.dbUrl, { useNewUrlParser: true });
console.log("Connected correctly to server");
const database: mongodb.Db = client.db(config.dbName);
app.use(session({
resave: false,
saveUninitialized: false,
secret: "secret123",
store: new MongoStore({
db: database,
}),
}));
app.use(router(database));
} catch (error) {
console.log("Error connecting to MongoDB", error);
}
})();
发现问题了,一直都是TS打字,@types/connect-mongo用的是旧版本的@types/mongodb ^2,我在我的项目中使用 @types/mongodb ^3。
当前方案,更新@types/connect-mongo包依赖到最新(覆盖@types/mongodb^ 2 到 ^3).
您需要在 package.json
内的 devDependencies 中添加以下行
"@types/mongodb": "^3"
我对 TS 比较陌生,在 new MongoStore()
参数中遇到了类型问题。通常我可以通过在 JS 中写入 db: database
作为参数来重用 Native MongoDB 连接,但是 TS 显示以下错误:
Argument of type '{ db: Db; }' is not assignable to parameter of type 'MongoUrlOptions | MogooseConnectionOptions | NativeMongoOptions | NativeMongoPromiseOptions'. Type '{ db: Db; }' is not assignable to type 'NativeMongoPromiseOptions'. Property 'dbPromise' is missing in type '{ db: Db; }'.
我试过写 new MongoStore({ db: database } as NativeMongoOptions)
,但这也无济于事。
[更新] 添加 as NativeMongoOptions
会产生差异错误:
Type '{ db: Db; }' is not assignable to type 'NativeMongoOptions'. Types of property 'db' are incompatible. Type 'import("/node_modules/@types/mongodb/index").Db' is not assignable to type 'import("/node_modules/@types/connect-mongo/node_modul...'. Property 'authenticate' is missing in type 'Db'.
这是我的代码问题还是 TS 类型问题?
下面是我的代码片段。
const MongoStore: connectMongo.MongoStoreFactory = connectMongo(session);
const app: express.Application = express();
enableMiddleware(app);
(async () => {
try {
const client: mongodb.MongoClient = await mongodb.MongoClient.connect(config.dbUrl, { useNewUrlParser: true });
console.log("Connected correctly to server");
const database: mongodb.Db = client.db(config.dbName);
app.use(session({
resave: false,
saveUninitialized: false,
secret: "secret123",
store: new MongoStore({
db: database,
}),
}));
app.use(router(database));
} catch (error) {
console.log("Error connecting to MongoDB", error);
}
})();
发现问题了,一直都是TS打字,@types/connect-mongo用的是旧版本的@types/mongodb ^2,我在我的项目中使用 @types/mongodb ^3。
当前方案,更新@types/connect-mongo包依赖到最新(覆盖@types/mongodb^ 2 到 ^3).
您需要在 package.json
内的 devDependencies 中添加以下行"@types/mongodb": "^3"