代码之家  ›  专栏  ›  技术社区  ›  Anthony Kong

向全局屏幕添加新方法时无法满足tslint的要求

  •  0
  • Anthony Kong  · 技术社区  · 6 年前

    https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock )不同浏览器中的功能。

    以下是我的尝试:

    export function lock(orientation: OrientationLockType) {
        const lockOrientation: (orientation: OrientationLockType) => boolean = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation || screen.orientation.lock;
        lockOrientation(orientation);
    }
    

    可视化代码标志 screen.mozLockOrientation Screen 类型。

    declare global {
        interface Screen {
            mozLockOrientation: (orientation: OrientationLockType) => boolean;
        }
    }
    

    但是tslint不喜欢 mozLockOrientation 因为它不是方法签名。

    所以我的问题是:

    1) 什么是方法签名?如何将上述内容转换为方法签名?

    2) 我怎样才能找到合适的型号 screen.orientation.lock orientation: { lock: (orientation: OrientationLockType) => boolean }; 但我想知道是否有更权威的消息来源。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Matt McCutchen    6 年前

    1) 下面是如何编写等效的方法签名:

    declare global {
        interface Screen {
            mozLockOrientation(orientation: OrientationLockType): boolean;
        }
    }
    

    2) 如果您按照MDN页面中的链接 the specification ,它有一个与TypeScript相当相似的WebIDL声明,并指示返回类型 Promise<void> 而不是 boolean