告诉我未来自我的最佳方法不参考多点的变量
在第2行中,我已经破坏了session
,因此应使用sessionwithnoid
。
我如何确保第2行之后我不会意外使用session
?
我尝试了删除会话
,但TS不喜欢它:'delete'在严格模式下无法在标识符上调用。
public async createOrUpdateOfflineSession(session: SessionInterface, store: Store) {
const { id, ...sessionWithNoId } = session;
const existingSession = await this.prisma.shopifyOfflineStoreSession.findUnique({
where: { shop: session.shop },
});
if (existingSession) {
await this.prisma.shopifyOfflineStoreSession.update({
where: { shop: session.shop },
data: { ...sessionWithNoId, sessionId: id },
data: { ...sessionWithNoId, sessionId: id },
} else {
await this.prisma.shopifyOfflineStoreSession.create({
data: {
...sessionWithNoId,
sessionId: id,
store: { connect: { id: store.id } },
},
});
}
}
In line 2, I've destructured session
and should be using sessionWithNoId
from henceforth.
How do I ensure I don't end up accidentally using session
after line 2?
I tried delete session
but TS doesn't like it: 'delete' cannot be called on an identifier in strict mode.
public async createOrUpdateOfflineSession(session: SessionInterface, store: Store) {
const { id, ...sessionWithNoId } = session;
const existingSession = await this.prisma.shopifyOfflineStoreSession.findUnique({
where: { shop: session.shop },
});
if (existingSession) {
await this.prisma.shopifyOfflineStoreSession.update({
where: { shop: session.shop },
data: { ...sessionWithNoId, sessionId: id },
data: { ...sessionWithNoId, sessionId: id },
} else {
await this.prisma.shopifyOfflineStoreSession.create({
data: {
...sessionWithNoId,
sessionId: id,
store: { connect: { id: store.id } },
},
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信最清洁的解决方案是将其毁灭在功能参数中。
现在,
会话
甚至在此功能主体的范围中都不存在。一个很好的观点!但是,它可以很容易地使用单个这代表了您功能的公共接口。通常,功能过载是何时函数有一些签名,但是只有一个函数时使用没有错。
For example:
And now the visible type for the function is:
See playground
I believe the cleanest solution is to just destructure it in the function parameters instead.
Now
session
doesn't even exist in the scope of this function body.An excellent point! But it's easily fixed with a single function overload signature that represents the public interface of your function. Typically function overloads are for when a function has a few signatures, but there's nothing wrong with using when there is only one.
For example:
And now the visible type for the function is:
See playground