用下面的代码在 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;
}
但是,TS 编译器认为 Lottie 是一个值不是类型。
TS2749: 'Lottie' refers to a value, but is being used as a type here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…