zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Typescript] OVerride external library Types

typescript library types External override
2023-09-14 08:59:11 时间
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;
}