简体   繁体   English

带有类型的 Javascript 库不是模块(React Native)

[英]Javascript Library with typings is Not a Module (React Native)

I'm trying to integrate a Javascript Library (Microsoft Playfab) into my React Native app as per recommended here .我正在尝试按照此处的建议将 Javascript 库 (Microsoft Playfab) 集成到我的 React Native 应用程序中。 The library comes with typings and has the structure as shownhere .图书馆配备了分型,并具有结构如图所示这里 I have a file playfabWrapper.ts with the following.我有一个文件playfabWrapper.ts ,内容如下。

/// <reference path="../../node_modules/playfab-web-sdk/src/Typings/PlayFab/PlayfabClientApi.d.ts" />

import Playfab from 'playfab-web-sdk/src/PlayFab/PlayFabClientApi.js';

function test(titleId: string, customId: string/*, success: (data: PlayFabClientModels.LoginResult) => void, error: (message: string) => void*/): void {
    Playfab.ClientApi.LoginWithCustomID({
        TitleId: titleId,
        CustomId: customId,
        CreateAccount: true,
    }, (result: any, problem: any) => {
        ...
    });
}

This gives me the error这给了我错误

Could not find a declaration file for module 'playfab-web-sdk/src/PlayFab/PlayFabClientApi.js'. '/Users/../project/node_modules/playfab-web-sdk/src/PlayFab/PlayFabClientApi.js' implicitly has an 'any' type.
Try `npm install @types/playfab-web-sdk` if it exists or add a new declaration (.d.ts) file containing `declare module 'playfab-web-sdk/src/PlayFab/PlayFabClientApi.js';`

Then I tried copying the typings from Typings/Playfab and pasting them in the same location as PlayFabClientApi.js .然后我尝试从 Typings/Playfab 复制类型并将它们粘贴到与PlayFabClientApi.js相同的位置。 This gives me the following error.这给了我以下错误。

File '/Users/../project/node_modules/playfab-web-sdk/src/PlayFab/PlayFabClientApi.d.ts' is not a module.

Interestingly, if I delete the import line my IDE can detect Playfab correctly but it is always undefined.有趣的是,如果我删除导入行,我的 IDE 可以正确检测到Playfab ,但它始终未定义。 Am I missing something?我错过了什么吗? Is this kind of library even possible to import?这种库甚至可以导入吗? I notice it doesn't have an index.d.ts , could that contribute to the issue?我注意到它没有index.d.ts ,这会导致这个问题吗? Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

Yes, if it doesn't have any type info (the index.d.ts file), you can't import it that way.是的,如果它没有任何类型信息( index.d.ts文件),则不能以这种方式导入。 You can try var PlayFab = require("...") (but that depends on your context).您可以尝试var PlayFab = require("...") (但这取决于您的上下文)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM