首页 > 其他分享 >[Typescript] OVerride external library Types

[Typescript] OVerride external library Types

时间:2023-02-23 15:23:07浏览次数:39  
标签:Typescript library animation export external file OVerride getAnimatingState typ

import { getAnimatingState } from "fake-animation-lib";
import { Equal, Expect } from "../helpers/type-utils";

const animatingState = getAnimatingState();

type tests = [
  Expect<
    Equal<
      typeof animatingState,
      "before-animation" | "animating" | "after-animation"
    >
  >
];

 

Current the function getAnimatingStatereturn is just string type. And it is coming from a extranal library fake-animation-lib.

export const getAnimatingState = (): string => {
  if (Math.random() > 0.5) {
    return "before-animation";
  }

  if (Math.random() > 0.5) {
    return "animating";
  }

  return "after-animation";
};

 

The way to override is by create a new *.d.ts file, you name name the file as you want, but need to keep .d.ts

// the new *.d.ts will override the previous declarion file
declare module "fake-animation-lib" {
  export type AnimatingState = "before-animation" | "animating" | "after-animation"
  export function getAnimatingState(): AnimatingState;
}

 

标签:Typescript,library,animation,export,external,file,OVerride,getAnimatingState,typ
From: https://www.cnblogs.com/Answer1215/p/17148090.html

相关文章