Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement/Web] Define undefined in PowerSyncCredentials #324

Open
guillempuche opened this issue Sep 30, 2024 · 0 comments
Open

[Improvement/Web] Define undefined in PowerSyncCredentials #324

guillempuche opened this issue Sep 30, 2024 · 0 comments

Comments

@guillempuche
Copy link
Contributor

guillempuche commented Sep 30, 2024

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.

@guillempuche guillempuche changed the title [Improvement/Web] Define undefined in PowerSyncCredentials type [Improvement/Web] Define undefined in PowerSyncCredentials Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant