Skip to content

[Improvement/Web] Define undefined in PowerSyncCredentials #324

Closed

Description

@guillempuche

Proposition

Modify expiresAt type in PowerSyncCredentials https://github.com/powersync-ja/powersync-js/blob/66412323c27a54d8623bba17c5e54832f5b60063/packages/common/src/client/connection/PowerSyncCredentials.ts#L1-L6 to

export interface PowerSyncCredentials {
  endpoint: string;
  token: string;
  expiresAt?: Date | undefined;
}

Problem

In fetchCredentials of PowerSyncBackendConnector like here

return {
endpoint: this.config.powersyncUrl,
token: session.access_token ?? '',
expiresAt: session.expires_at ? new Date(session.expires_at * 1000) : undefined
};

I'm using a restrictive Tsconfig, exactOptionalPropertyTypes. And I'm also using satisfies to be safer:

    return {
      endpoint: this.config.powersyncUrl,
      token: session.access_token ?? '',
      expiresAt: session.expires_at ? new Date(session.expires_at * 1000) : undefined
    } satisfies PowerSyncCredentials; // <-- Compile error, more belo

I get this compile error.

Type '{ endpoint: string; token: string; expiresAt: Date | undefined; }' does not satisfy the expected type 'PowerSyncCredentials'.
  Types of property 'expiresAt' are incompatible.
    Type 'Date | undefined' is not assignable to type 'Date'.
      Type 'undefined' is not assignable to type 'Date'.ts(1360)

Provisional Solution

const credentials: PowerSyncCredentials = {
	endpoint: config.powerSyncUrl,
	token: session.value.access_token ?? '',
}

if (session.value.expires_at !== undefined) {
	credentials.expiresAt = new Date(
		session.value.expires_at * 1000,
	)
}

return credentials satisfies PowerSyncCredentials

Suggestion

In all your tsconfig files, start using exactOptionalPropertyTypes for safer code.

Activity

  1. changed the title [-][Improvement/Web] Define `undefined` in PowerSyncCredentials type[/-] [+][Improvement/Web] Define `undefined` in `PowerSyncCredentials`[/+] on Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions