告诉我未来自我的最佳方法不参考多点的变量

发布于 2025-02-07 21:14:08 字数 911 浏览 2 评论 0原文

在第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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

<逆流佳人身旁 2025-02-14 21:14:08

我相信最清洁的解决方案是将其毁灭在功能参数中。

public async createOrUpdateOfflineSession(
  { id, ...sessionWithNoId }: SessionInterface,
  store: Store
) {
  //...
}

现在,会话甚至在此功能主体的范围中都不存在。


一个缺点是该函数的用户将不再看到session:SessionInterface在其提示中 - &nbsp; @hittingonme

一个很好的观点!但是,它可以很容易地使用单个这代表了您功能的公共接口。通常,功能过载是何时函数有一些签名,但是只有一个函数时使用没有错。

For example:

async function createOrUpdateOfflineSession(session: SessionInterface, store: Store): Promise<void>
async function createOrUpdateOfflineSession({ id, ...sessionWithNoId }: SessionInterface, store: Store): Promise<void> {
    //...
}

And now the visible type for the function is:

function createOrUpdateOfflineSession(session: SessionInterface, store: Store): Promise<void>

See playground

I believe the cleanest solution is to just destructure it in the function parameters instead.

public async createOrUpdateOfflineSession(
  { id, ...sessionWithNoId }: SessionInterface,
  store: Store
) {
  //...
}

Now session doesn't even exist in the scope of this function body.


A downside is that users of the function will no longer see session: SessionInterface in their hints – @hittingonme

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:

async function createOrUpdateOfflineSession(session: SessionInterface, store: Store): Promise<void>
async function createOrUpdateOfflineSession({ id, ...sessionWithNoId }: SessionInterface, store: Store): Promise<void> {
    //...
}

And now the visible type for the function is:

function createOrUpdateOfflineSession(session: SessionInterface, store: Store): Promise<void>

See playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文