问题
I'm working on project, where we're using Stripe library for Node. We also want to use TypeScript on this project.
I've figured out that Stripe isn't providing official TypeScript definitions but I've found some community definitions @types/stripe on NPM. So I installed them and after a while I got an error:
Property 'sources' does not exist on type 'Stripe'.
Well there are missing some definitions, for example for this stripe.sources-related functionality.
I want to add missing definitions locally. So I need to extend this file: @types/stripe/index.d.ts
I think that for the problem above I need:
- to add property
sources: Stripe.resources.Sources;
to classStripe
, - to add class
Sources
to namespaceresources
, - to add missing function declarations to class
Sources
.
The problem is that I really don't know how. How should the .d.ts
file with extensions look like? I've made many attempts according some examples and TypeScript docs but it always doesn't work. Do you have any idea?
回答1:
I don't believe there's a way to augment the export-assigned Stripe
class; the problem is similar to this open issue about augmenting a default-exported class. At this time, since you can't use augmentation, you'll have to fork the @types/stripe
definitions for your project, and then you may as well make all the desired changes that way.
回答2:
I think my colleague has found a solution that works for me. Here is how he made it:
import ST from 'stripe'
declare module 'stripe' {
namespace sources {
interface ISource extends IResourceObject {
...
}
interface ISourceCreationData {
...
}
}
namespace resources {
class Sources {
create(data: sources.ISourceCreationData): Promise<sources.ISource>;
retrieve(source: string, client_secret?: string): Promise<sources.ISource>;
}
}
class Stripe extends ST {
sources: ST.resources.Sources;
}
}
来源:https://stackoverflow.com/questions/52295518/stripe-typescript-how-to-extend-types-stripe-definitions-for-stripe-node