简体   繁体   English

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

[英]How to import default export type in TypeScript?

I want to declare window.lottie with the below code.我想用下面的代码声明 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;
}

But there is an error.但是有一个错误。

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

So how to import default export type in TypeScript?那么如何在 TypeScript 中导入默认导出类型呢?

declare const Lottie:LottiePlayer tells us that module lottie-web exports an object of type LottiePlayer , not a type. declare const Lottie:LottiePlayer告诉我们模块lottie-web导出一个LottiePlayer类型的LottiePlayer而不是类型。

It looks like you really want window.lottie to be of type LottiePlayer .看起来您真的希望window.lottie的类型为LottiePlayer

The type LottiePlayer is not explicitly exported from lottie-web . LottiePlayer类型不是从lottie-web显式导出的。 However, using the typeof keyword, we can sidestep this as follows:但是,使用typeof关键字,我们可以避开这一点,如下所示:

import Lottie from 'lottie-web';

declare interface Window {
  lottie: typeof Lottie; //LottiePlayer
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM