【问题标题】:How can I import from a library when there is no type defined for that single value当没有为该单个值定义类型时,如何从库中导入
【发布时间】:2021-11-26 21:00:24
【问题描述】:

我正在开发一个使用 react-spring 的组件库的项目。我无法控制组件库,因此我无法控制它使用 react-spring v8 的事实

我在 PR 上找到了 comment,它告诉我如何在测试环境中运行时修复动画。它需要我导入 Globals 对象。当我尝试通过import { Globals } 这样做时,我收到错误消息Module '"react-spring"' has no exported member 'Globals'.ts(2305),因为我的项目正在使用 Typescript(其中的说明适用于 JS)

当我查看我的 node_modules 目录中 react-spring 的代码时,我可以看到 Globals 对象肯定在主代码和模块代码中都被导出了。

web.js(主要):

export { apply, config, update, extendedAnimated as animated, extendedAnimated as a, interpolate$1 as interpolate, Globals, useSpring, useTrail, useTransition, useChain, useSprings };

web.cjs.js(模块):

exports.Globals = Globals;

但是,当我查看 types provide by react-spring 时,Globals 没有任何内容,我担心这会导致我无法导入 Globals,以便我可以在测试期间按照指示提供模拟 requestAnimationFrame。

我对 Typescript 很陌生,所以我可能误解了某些东西,我已经尝试 Google 解决方案,但我几乎只能找出如何处理一个没有类型的库关于如何修复库提供的类型中的“漏洞”的所有内容。

这是一个显示相同错误的 CodeSandbox(必须正在编辑沙箱。不在预览模式下显示。

【问题讨论】:

    标签: typescript react-spring


    【解决方案1】:

    这看起来像是react-spring@8 中的 Typescript 错误,因为是的,Globals 已导出。如果您有兴趣针对 v8 分支进行 PR,我们可以发布此修复程序。

    如果您想立即修复,可以添加:

    declare module "react-spring" {
      interface Globals {
        injectFrame: () => void;
      }
    
      export const Globals: Globals;
    }
    

    通常到*.d.ts 文件或您需要的地方。这将为您提供所需的功能,您可以继续在需要的地方添加更多功能。否则你可以使用// @ts-expect-error 来消除错误。

    这是一个展示分辨率的沙盒 - https://codesandbox.io/s/admiring-frog-5enu2?file=/src/App.tsx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 2020-09-02
      • 2021-09-08
      • 1970-01-01
      • 2019-10-05
      • 2023-02-24
      • 2019-01-22
      • 2017-02-10
      相关资源
      最近更新 更多