【问题标题】:How to import default export type in TypeScript?如何在 TypeScript 中导入默认导出类型?
【发布时间】:2020-06-08 08:26:00
【问题描述】:

我想用下面的代码声明 window.lottie。

// node_modules/lottie-web/index.d.ts
type LottiePlayer = {
    play(name?: string): void;
    stop(name?: string): void;
    setSpeed(speed: number, name?: string): void;
    setDirection(direction: AnimationDirection, name?: string): void;
    searchAnimations(animationData?: any, standalone?: boolean, renderer?: string): void;
    loadAnimation(params: AnimationConfigWithPath | AnimationConfigWithData): AnimationItem;
    destroy(name?: string): void;
    registerAnimation(element: Element, animationData?: any): void;
    setQuality(quality: string | number): void;
    setLocationHref(href: string): void;
};

declare const Lottie: LottiePlayer;

export default Lottie;
//src/type.d.ts
import Lottie from 'lottie-web';

declare interface Window {
  lottie: Lottie;
}

但是有一个错误。

TS2749: 'Lottie' refers to a value, but is being used as a type here.

那么如何在 TypeScript 中导入默认导出类型呢?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    declare const Lottie:LottiePlayer 告诉我们模块lottie-web 导出了一个LottiePlayer 类型的对象,不是 类型。

    看起来您确实希望 window.lottie 的类型为 LottiePlayer。

    LottiePlayer 类型没有从lottie-web 显式导出。但是,使用typeof 关键字,我们可以避开这一点,如下所示:

    import Lottie from 'lottie-web';
    
    declare interface Window {
      lottie: typeof Lottie; //LottiePlayer
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 2019-04-03
      • 2018-10-14
      • 2019-04-30
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2020-05-23
      相关资源
      最近更新 更多