Repository navigation
Closed
Description
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
powersync-js/demos/react-supabase-todolist/src/library/powersync/SupabaseConnector.ts
Lines 99 to 103 in 6641232
| 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 beloI 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 PowerSyncCredentialsSuggestion
In all your tsconfig files, start using exactOptionalPropertyTypes for safer code.
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-][Improvement/Web] Define `undefined` in PowerSyncCredentials type[/-][+][Improvement/Web] Define `undefined` in `PowerSyncCredentials`[/+]