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

问题描述 投票:0回答:1

我想用下面的代码声明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个回答
0
投票

declare const Lottie:LottiePlayer告诉我们模块lottie-web导出类型为LottiePlayerobjectnot类型。

类型LottiePlayer未从lottie-web显式导出。但是,使用typeof关键字,我们可以如下进行回避:

import Lottie from 'lottie-web';

declare interface Window {
  lottie: typeof Lottie; //LottiePlayer
}
© www.soinside.com 2019 - 2024. All rights reserved.