我正在尝试在我的项目中导入一个javascript模块。我正在尝试两种不同的配置,因为提供模块(条带)的供应商具有两种不同的模块格式。我收到如下所示的错误:
tsconfig.json:
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "ES6",
"outDir": "./wwwroot/js",
"lib": [ "ES2018", "DOM", "ES5", "ES2015", "ES2016", "ESNext" ],
"module": "ES2015",
"moduleResolution": "Node",
"typeRoots": [ "./node_modules/@types/", "./node_modules/@stripe/" ]
},
"exclude": [
"wwwroot"
]
}
import { loadStripe } from '../node_modules/@stripe/stripe-js';
错误消息:
index.d.ts不是一个模块。
文件node_modules/@stripe/stripe-js/types/index.d.ts看起来像这样:
///<reference path='./stripe-js/index.d.ts' />
///<reference path='./api/index.d.ts' />
declare module '@stripe/stripe-js' {
const loadStripe: (
publishableKey: string,
options?: StripeConstructorOptions | undefined
) => Promise<Stripe | null>;
}
interface Window {
// Stripe.js must be loaded directly from https://js.stripe.com/v3, which
// places a `Stripe` object on the window
Stripe?: import('@stripe/stripe-js').StripeConstructor;
}
我很可能在tsconfig
中缺少可以理解declare module
的配置选项。我写的项目中的其他模块使用export class...
。我该如何更改tsconfig
以适应此语法?
import { loadStripe } from '../node_modules/@stripe/stripe-js/pure';
文件node_modules/@stripe/stripe-js/pure.d.ts看起来像这样:
///<reference path='./types/index.d.ts' />
export const loadStripe: typeof import('@stripe/stripe-js').loadStripe & {
setLoadParameters: (params: {advancedFraudSignals: boolean}) => void;
};
如果我如上图所示进行导入,则项目构建失败,但在运行时失败404,因为它正在寻找缺少.js扩展名的文件:
https://localhost:5017/node_modules/@stripe/stripe-js/pure
我相信选项2的问题与使用2年以上TypeScript的this issue有关。我今年53岁,我没有理由相信这个问题将在我的一生中得到解决。就是说,这是一种选择,如果有已知的解决方法,我很乐意尝试。
类型found here已过期。条纹claims提供了类型,我想使用它们。
答案 0 :(得分:0)
node_modules/@stripe/stripe-js/types/index.d.ts
声明了一个外部环境模块,应尽可能避免这种做法。希望他们能解决他们的类型声明。
不可能通过相对路径导入环境外部模块。
但是,您自己进口的货物也是一种惯例,应尽可能避免。
请 避免 使用相对路径导入您的依赖项,即使与大多数模块一样,它们也不是作为外部环境输入的。
解决方案很简单:
import {loadStripe} from '@stripe/stripe-js';