Skip to content

Commit ff4f413

Browse files
committed
fix types
1 parent 72e3258 commit ff4f413

File tree

9 files changed

+60
-47
lines changed

9 files changed

+60
-47
lines changed

packages/clerk-js/src/core/clerk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type {
2929
AuthenticateWithGoogleOneTapParams,
3030
AuthenticateWithMetamaskParams,
3131
AuthenticateWithOKXWalletParams,
32+
CheckoutSignalValue,
3233
Clerk as ClerkInterface,
3334
ClerkAPIError,
3435
ClerkAuthenticateWithWeb3Params,
@@ -51,7 +52,6 @@ import type {
5152
JoinWaitlistParams,
5253
ListenerCallback,
5354
NavigateOptions,
54-
NullableCheckoutSignal,
5555
OrganizationListProps,
5656
OrganizationProfileProps,
5757
OrganizationResource,
@@ -351,7 +351,7 @@ export class Clerk implements ClerkInterface {
351351
return Clerk._apiKeys;
352352
}
353353

354-
__experimental_checkout(options: __experimental_CheckoutOptions): NullableCheckoutSignal {
354+
__experimental_checkout(options: __experimental_CheckoutOptions): CheckoutSignalValue {
355355
if (!this._checkout) {
356356
this._checkout = (params: any) => createCheckoutInstance(this, params);
357357
}

packages/clerk-js/src/core/modules/checkout/instance.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { __experimental_CheckoutOptions, NullableCheckoutSignal } from '@clerk/types';
1+
import type { __experimental_CheckoutOptions, CheckoutSignalValue } from '@clerk/types';
22

33
import { CheckoutFuture, createSignals } from '@/core/resources/CommerceCheckout';
44

@@ -19,7 +19,7 @@ const cache = new Map<CheckoutKey, { resource: CheckoutFuture; signals: ReturnTy
1919
/**
2020
* Create a checkout instance with the given options
2121
*/
22-
function createCheckoutInstance(clerk: Clerk, options: __experimental_CheckoutOptions): NullableCheckoutSignal {
22+
function createCheckoutInstance(clerk: Clerk, options: __experimental_CheckoutOptions): CheckoutSignalValue {
2323
const { for: forOrganization, planId, planPeriod } = options;
2424

2525
if (!clerk.isSignedIn || !clerk.user) {
@@ -40,7 +40,7 @@ function createCheckoutInstance(clerk: Clerk, options: __experimental_CheckoutOp
4040
if (cache.has(checkoutKey)) {
4141
return (
4242
cache.get(checkoutKey) as { resource: CheckoutFuture; signals: ReturnType<typeof createSignals> }
43-
).signals.computedSignal();
43+
).signals.computedSignal() as CheckoutSignalValue;
4444
}
4545

4646
const signals = createSignals();
@@ -51,7 +51,7 @@ function createCheckoutInstance(clerk: Clerk, options: __experimental_CheckoutOp
5151
planPeriod,
5252
});
5353
cache.set(checkoutKey, { resource: checkout, signals });
54-
return signals.computedSignal();
54+
return signals.computedSignal() as CheckoutSignalValue;
5555
}
5656

5757
export { createCheckoutInstance };

packages/clerk-js/src/core/resources/CommerceCheckout.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { retry } from '@clerk/shared/retry';
22
import type {
3+
CheckoutFutureResource,
34
CheckoutFutureResourceLax,
45
CheckoutSignalValue,
56
CommerceCheckoutJSON,
@@ -128,15 +129,16 @@ export const createSignals = () => {
128129
const resourceSignal = signal<{ resource: CheckoutFuture | null }>({ resource: null });
129130
const errorSignal = signal<{ error: unknown }>({ error: null });
130131
const fetchSignal = signal<{ status: 'idle' | 'fetching' }>({ status: 'idle' });
131-
// @ts-expect-error - CheckoutSignal is not yet defined
132-
const computedSignal: CheckoutSignal = computed(() => {
133-
const resource = resourceSignal().resource;
134-
const error = errorSignal().error;
135-
const fetchStatus = fetchSignal().status;
136-
137-
const errors = errorsToParsedErrors(error);
138-
return { errors: errors, fetchStatus, checkout: resource };
139-
});
132+
const computedSignal = computed<Omit<CheckoutSignalValue, 'checkout'> & { checkout: CheckoutFutureResource | null }>(
133+
() => {
134+
const resource = resourceSignal().resource;
135+
const error = errorSignal().error;
136+
const fetchStatus = fetchSignal().status;
137+
138+
const errors = errorsToParsedErrors(error);
139+
return { errors: errors, fetchStatus, checkout: resource };
140+
},
141+
);
140142

141143
return { resourceSignal, errorSignal, fetchSignal, computedSignal };
142144
};
@@ -208,6 +210,9 @@ export class CheckoutFuture implements CheckoutFutureResourceLax {
208210

209211
async confirm(params: ConfirmCheckoutParams): Promise<{ error: unknown }> {
210212
return this.runAsyncResourceTask('confirm', async () => {
213+
if (!this.resource.id) {
214+
throw new Error('Clerk: Call `start` before `confirm`');
215+
}
211216
await this.resource.confirm(params);
212217
});
213218
}
@@ -233,7 +238,6 @@ function createRunAsyncResourceTask(
233238
startBatch();
234239
signals.errorSignal({ error: null });
235240
signals.fetchSignal({ status: 'fetching' });
236-
// signals.resourceSignal({ resource: null });
237241
beforeTask?.();
238242
endBatch();
239243
startBatch();
@@ -243,7 +247,6 @@ function createRunAsyncResourceTask(
243247
return { error: null };
244248
} catch (err) {
245249
signals.errorSignal({ error: err });
246-
endBatch();
247250
return { error: err };
248251
} finally {
249252
pendingOperations.delete(operationType);

packages/clerk-js/src/ui/components/Checkout/CheckoutForm.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { __experimental_useCheckout as useCheckout } from '@clerk/shared/react';
2-
import type { CommerceMoneyAmount, CommercePaymentSourceResource, ConfirmCheckoutParams } from '@clerk/types';
2+
import type {
3+
CommerceMoneyAmount,
4+
CommercePaymentSourceResource,
5+
ConfirmCheckoutParams,
6+
RemoveFunctions,
7+
} from '@clerk/types';
38
import { useMemo, useState } from 'react';
49

510
import { Card } from '@/ui/elements/Card';
@@ -347,10 +352,9 @@ const ExistingPaymentSourceForm = withCardStateProvider(
347352

348353
const { payWithExistingPaymentSource } = useCheckoutMutations();
349354
const card = useCardState();
350-
const [selectedPaymentSource, setSelectedPaymentSource] = useState<CommercePaymentSourceResource | undefined>(
351-
// @ts-expect-error - paymentSource is missing functions
352-
paymentSource || paymentSources.find(p => p.isDefault),
353-
);
355+
const [selectedPaymentSource, setSelectedPaymentSource] = useState<
356+
RemoveFunctions<CommercePaymentSourceResource> | undefined
357+
>(paymentSource || paymentSources.find(p => p.isDefault));
354358

355359
const options = useMemo(() => {
356360
return paymentSources.map(source => {

packages/clerk-js/src/ui/components/PaymentSources/PaymentSourceRow.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import type { CommercePaymentSourceResource } from '@clerk/types';
1+
import type { CommercePaymentSourceResource, RemoveFunctions } from '@clerk/types';
22

33
import { Badge, descriptors, Flex, Icon, localizationKeys, Text } from '../../customizables';
44
import { CreditCard, GenericPayment } from '../../icons';
55

6-
export const PaymentSourceRow = ({ paymentSource }: { paymentSource: CommercePaymentSourceResource }) => {
6+
export const PaymentSourceRow = ({
7+
paymentSource,
8+
}: {
9+
paymentSource: RemoveFunctions<CommercePaymentSourceResource>;
10+
}) => {
711
return (
812
<Flex
913
sx={{ overflow: 'hidden' }}

packages/react/src/stateProxy.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { inBrowser } from '@clerk/shared/browser';
22
import type {
33
CheckoutFutureResource,
4+
CheckoutSignalValue,
45
Clerk,
56
CommerceSubscriptionPlanPeriod,
67
Errors,
78
ForPayerType,
8-
NullableCheckoutSignal,
99
State,
1010
} from '@clerk/types';
1111

@@ -121,7 +121,7 @@ export class StateProxy implements State {
121121
};
122122
}
123123

124-
private buildCheckoutProxy(params: CheckoutSignalProps): NullableCheckoutSignal {
124+
private buildCheckoutProxy(params: CheckoutSignalProps): CheckoutSignalValue {
125125
const gateProperty = this.gateProperty.bind(this);
126126
const targetCheckout = () => this.checkout(params);
127127
const target = () => targetCheckout().checkout as CheckoutFutureResource;
@@ -132,37 +132,39 @@ export class StateProxy implements State {
132132
global: null,
133133
},
134134
fetchStatus: 'idle' as const,
135-
// @ts-expect-error - CheckoutFutureResource is not yet defined
136135
checkout: {
137136
get status() {
138-
return gateProperty(target, 'status', 'needs_initialization');
137+
return gateProperty(target, 'status', 'needs_initialization') as 'needs_initialization';
139138
},
140139
get externalClientSecret() {
141-
return gateProperty(target, 'externalClientSecret', null);
140+
return gateProperty(target, 'externalClientSecret', null) as null;
142141
},
143142
get externalGatewayId() {
144-
return gateProperty(target, 'externalGatewayId', null);
143+
return gateProperty(target, 'externalGatewayId', null) as null;
145144
},
146145
get paymentSource() {
147-
return gateProperty(target, 'paymentSource', null);
146+
return gateProperty(target, 'paymentSource', null) as null;
148147
},
149148
get plan() {
150-
return gateProperty(target, 'plan', null);
149+
return gateProperty(target, 'plan', null) as null;
151150
},
152151
get planPeriod() {
153-
return gateProperty(target, 'planPeriod', null);
152+
return gateProperty(target, 'planPeriod', null) as null;
154153
},
155154
get totals() {
156-
return gateProperty(target, 'totals', null);
155+
return gateProperty(target, 'totals', null) as null;
157156
},
158157
get isImmediatePlanChange() {
159-
return gateProperty(target, 'isImmediatePlanChange', false);
158+
return gateProperty(target, 'isImmediatePlanChange', false) as null;
160159
},
161160
get freeTrialEndsAt() {
162-
return gateProperty(target, 'freeTrialEndsAt', null);
161+
return gateProperty(target, 'freeTrialEndsAt', null) as null;
163162
},
164163
get payer() {
165-
return gateProperty(target, 'payer', null);
164+
return gateProperty(target, 'payer', null) as null;
165+
},
166+
get planPeriodStart() {
167+
return gateProperty(target, 'planPeriodStart', null) as null;
166168
},
167169

168170
start: this.gateMethod<ReturnType<typeof target>, 'start'>(target, 'start'),

packages/types/src/clerk.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,8 @@ export interface CheckoutSignalValue {
8888
checkout: CheckoutFutureResource;
8989
}
9090

91-
export type NullableCheckoutSignal = Omit<CheckoutSignalValue, 'checkout'> & {
92-
checkout: CheckoutFutureResource | null;
93-
};
94-
9591
export interface CheckoutSignal {
96-
(): NullableCheckoutSignal;
92+
(): CheckoutSignalValue;
9793
}
9894

9995
export type __experimental_CheckoutOptions = {
@@ -102,7 +98,7 @@ export type __experimental_CheckoutOptions = {
10298
planId: string;
10399
};
104100

105-
type __experimental_CheckoutFunction = (options: __experimental_CheckoutOptions) => NullableCheckoutSignal;
101+
type __experimental_CheckoutFunction = (options: __experimental_CheckoutOptions) => CheckoutSignalValue;
106102

107103
/**
108104
* @inline

packages/types/src/commerce.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { DeletedObjectResource } from './deletedObject';
22
import type { ClerkPaginatedResponse, ClerkPaginationParams } from './pagination';
33
import type { ClerkResource } from './resource';
44
import type { CommerceFeatureJSONSnapshot } from './snapshots';
5-
import type { ForceNull, RemoveFunctions } from './utils';
5+
import type { ForceNull, Prettify, RemoveFunctions } from './utils';
66

77
type WithOptionalOrgType<T> = T & {
88
/**
@@ -1365,11 +1365,11 @@ export interface CheckoutFutureProperties {
13651365
/**
13661366
* The payment source being used for the checkout, such as a credit card or bank account.
13671367
*/
1368-
paymentSource: RemoveFunctions<CommercePaymentSourceResource> | null;
1368+
paymentSource: Prettify<RemoveFunctions<CommercePaymentSourceResource>> | null;
13691369
/**
13701370
* The subscription plan details for the checkout.
13711371
*/
1372-
plan: RemoveFunctions<CommercePlanResource>;
1372+
plan: Prettify<RemoveFunctions<CommercePlanResource>>;
13731373
/**
13741374
* The billing period for the plan.
13751375
*/
@@ -1398,7 +1398,7 @@ export interface CheckoutFutureProperties {
13981398
* <ClerkProvider clerkJsVersion="x.x.x" />
13991399
* ```
14001400
*/
1401-
payer: RemoveFunctions<CommercePayerResource>;
1401+
payer: Prettify<RemoveFunctions<CommercePayerResource>>;
14021402
}
14031403

14041404
type CheckoutPropertiesPerStatus =

packages/types/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ export type Override<T, U> = Omit<T, keyof U> & U;
127127
export type RemoveFunctions<T extends object> = {
128128
[K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: T[K];
129129
};
130+
131+
export type Prettify<T> = {
132+
[K in keyof T]: T[K];
133+
} & {};

0 commit comments

Comments
 (0)