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

你。 提交于 2021-02-10 20:14:10

问题


I'm trying to integrate a Javascript Library (Microsoft Playfab) into my React Native app as per recommended here. The library comes with typings and has the structure as shown here. I have a file playfabWrapper.ts with the following.

/// <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. 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. 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? Any help would be appreciated. Thanks.


回答1:


Yes, if it doesn't have any type info (the index.d.ts file), you can't import it that way. You can tryvar PlayFab = require("...") (but that depends on your context).



来源:https://stackoverflow.com/questions/59700857/javascript-library-with-typings-is-not-a-module-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!