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