[Typescript] OVerride external library Types

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;
}

 

posted @ 2023-02-23 15:12  Zhentiw  阅读(188)  评论(0编辑  收藏  举报