繁体   English   中英

TypeScript中如何修改内置类型

[英]How to modify built-in types in TypeScript

我被困在我想修改全局类型的场景中。 试图更改Element.prototype.animate的签名(希望使其成为可选的)function。

试过这样的事情


declare global {
  interface Element {
    animate?: Animatable['animate'];
  }
}

但是,它会导致以下 TS 错误:

Interface 'Element' incorrectly extends interface 'Animatable'.  
  Types of property 'animate' are incompatible.
    Type '((keyframes: PropertyIndexedKeyframes | Keyframe[] | null, options?: number | KeyframeAnimationOptions | undefined) => Animation) | undefined' is not assignable to type '(keyframes: PropertyIndexedKeyframes | Keyframe[] | null, options?: number | KeyframeAnimationOptions | undefined) => Animation'.
      Type 'undefined' is not assignable to type '(keyframes: PropertyIndexedKeyframes | Keyframe[] | null, options?: number | KeyframeAnimationOptions | undefined) => Animation'.

如何在不关闭任何类型的 TS 检查的情况下解决这个问题?

这对你有用吗...

type OptionalAnimate = Omit<Element, "animate"> & {
    animate?: Animatable['animate'];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM