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

时间:2020-06-08 08:26:00

标签: typescript

我想用以下代码声明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中导入默认的导出类型?

1 个答案:

答案 0 :(得分:3)

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
}