diff --git a/types/react-stripe-elements/index.d.ts b/types/react-stripe-elements/index.d.ts index 8713d5828a..91e64d6117 100644 --- a/types/react-stripe-elements/index.d.ts +++ b/types/react-stripe-elements/index.d.ts @@ -3,6 +3,7 @@ // Definitions by: dan-j // Santiago Doldan // sonnysangha +// Andrew Goh Yisheng // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 @@ -23,23 +24,14 @@ export namespace ReactStripeElements { error?: { decline_code?: string }; }; - interface StripeProviderProps { - apiKey: string; - } - - interface StripeProps { - // I'm not sure what the definition for this is - createSource(): void; - - createToken(options?: TokenOptions): Promise; - } + type StripeProviderProps = { apiKey: string; stripe?: never; } | { apiKey?: never; stripe: stripe.Stripe; }; interface InjectOptions { withRef?: boolean; } interface InjectedStripeProps { - stripe?: StripeProps; + stripe?: stripe.Stripe; } interface ElementProps extends ElementsOptions { diff --git a/types/react-stripe-elements/react-stripe-elements-tests.tsx b/types/react-stripe-elements/react-stripe-elements-tests.tsx index e737914e31..d2cb66c6a3 100644 --- a/types/react-stripe-elements/react-stripe-elements-tests.tsx +++ b/types/react-stripe-elements/react-stripe-elements-tests.tsx @@ -107,18 +107,26 @@ interface ComponentProps { tokenCallback(token: PatchedTokenResponse): void; } -class WrappedComponent extends React.Component { +class WrappedComponent extends React.Component< + ComponentProps & InjectedStripeProps +> { onSubmit = () => { - this.props.stripe!.createToken({ - name: '', - address_line1: '', - address_line2: '', - address_city: '', - address_state: '', - address_zip: '', - address_country: '', - currency: '', - }).then((response: PatchedTokenResponse) => this.props.tokenCallback(response)); + const elements = this.props.stripe!.elements(); + const card = elements.create('card', { hidePostalCode: true }); + this.props.stripe! + .createToken(card, { + name: '', + address_line1: '', + address_line2: '', + address_city: '', + address_state: '', + address_zip: '', + address_country: '', + currency: '', + }) + .then((response: PatchedTokenResponse) => + this.props.tokenCallback(response) + ); } isFormValid = () => { @@ -170,3 +178,13 @@ const ElementsDefaultPropsTest: React.SFC = () => ( ); + +/** + * StripeProvider should either receive `apiKey` or `stripe`, but not both. + * See: https://github.com/stripe/react-stripe-elements/blob/d30b32b6b8df282dd8880a3521667c371e90083f/src/components/Provider.js#L83-L86 + */ +const TestStripeProviderProps1: React.SFC = () => ; + +const TestStripeProviderProps2: React.SFC<{ + stripe: stripe.Stripe; +}> = props => ;