gpt4 book ai didi

node.js - 创建构造函数签名

转载 作者:行者123 更新时间:2023-12-02 13:16:51 24 4
gpt4 key购买 nike

我是 typescript 新手,正在将现有的 js 代码迁移到 ts 代码。

显然connect-session-firebase没有自定义类型,所以我决定创建一个..

这就是我在代码中使用它的方式

import FirebaseSession from 'connect-session-firebase'

app.use(
session({
store: new FirebaseSession({
database: context.realtimeDB
}),
name: '__session', // Must use this name for firebase hosting
secret: 'kkkkj9h9', // TODO: Move to ENV
resave: true,
saveUninitialized: true
})
)

这是我打字的地方

declare module "connect-session-firebase" {
import * as admin from 'firebase-admin'
import { Request, Response, NextFunction } from "express";

interface firebaseSession {
database: admin.database.Database | admin.firestore.Firestore,
sessions?: string,
reapInterva?: string,
errorIfSessionNotFound?: any
}
}

我知道这是错误的,因为这里 store: new firebaseSession({ 我收到以下错误

this expression is not constructable.
Type 'typeof import("connect-session-firebase")' has no construct signatures.

有人可以告诉我我做错了什么以及如何解决它吗?

更新:我将firebaseSession更新为FirebaseSession并尝试使用new()但仍然没有运气

declare module "connect-session-firebase" {
import * as admin from 'firebase-admin'
import { Request, Response, NextFunction } from "express";

interface FirebaseSession {
new(
database: admin.database.Database | admin.firestore.Firestore,
sessions?: string,
reapInterva?: string,
errorIfSessionNotFound?: any)
}
}

但同样的错误

更新:这是我更新的代码:

import FirebaseSession from 'connect-session-firebase'

app.use(
session({
store: new FirebaseSession({
database: context.realtimeDB
}),
name: '__session', // Must use this name for firebase hosting
secret: 'kkkkj9h9', // TODO: Move to ENV
resave: true,
saveUninitialized: true
})
)

类型 connect-session-firebase

import * as admin from 'firebase-admin'

declare class FirebaseSession {
constructor(options: FirebaseSession.Options)
}


declare namespace FirebaseSession {
export interface Options {
database: admin.database.Database | admin.firestore.Firestore,
sessions?: string,
reapInterva?: string,
errorIfSessionNotFound?: any
}
}


export = FirebaseSession

注意虽然这不会在编译时引发错误,但它会创建如下所示的等效 JS 代码

const connect_session_firebase_1 = __importDefault(require("connect-session-firebase"));
app.use(express_session_1.default({
store: new connect_session_firebase_1.default({
database: context.realtimeDB
}),
name: '__session',
secret: 'kkkkj9h9',
resave: true,
saveUninitialized: true
}));

抛出错误connect_session_firebase_1.FirebaseSession不是构造函数

Cannot read property prototype of undefined.

最佳答案

你可以这样尝试吗?

1) 创建一个 type.d.ts


declare module "connect-session-firebase" {
import * as admin from 'firebase-admin'
export interface OptionsShape {
database: admin.database.Database | admin.firestore.Firestore,
sessions?: string,
reapInterva?: string,
errorIfSessionNotFound?: any
}

export interface FirebaseSession {
new(n: OptionsShape): void
}

export class FirebaseSession {
constructor(n: OptionsShape);
}

export default FirebaseSession;
}

2)导入你的模块并使用它(应该输入它并且不要提示输入任何内容

import FirebaseSession from 'connect-session-firebase'

var firebase = new FirebaseSession({
//params
});

编辑:正如我在评论中所说,您可以像这样修复编译,但现在的问题是由您为库创建正确的类型。我不知道这个库,所以我不知道 new FirebaseSession({}) 的返回值是什么,也不知道 FirebaseSession 有哪些属性和方法。 .

您必须阅读该包并根据它调整您的 types.d.ts,这样它才能完美运行。您可能可以从包创建者那里获得帮助,并在完成后创建一个 @types/connect-session-firebase 。但如果只是为了编译器工作,你可以继续打补丁(但不推荐)

declare module "connect-session-firebase" {
import * as admin from 'firebase-admin'
import { Store, MemoryStore } from 'express-session';
export interface OptionsShape {
database?: admin.database.Database | admin.firestore.Firestore,
sessions?: string,
reapInterva?: string,
errorIfSessionNotFound?: any
}

export interface FirebaseSession {
new(n: OptionsShape)
}

export class FirebaseSession extends MemoryStore {
constructor(n: OptionsShape);
}

export default FirebaseSession;
}

关于node.js - 创建构造函数签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com