从包类型扩展命名空间

2024-02-15

我在这里尝试从包类型扩展命名空间,@typings/fullcalendar.

/// <reference path="./types/fullcalendar" />

import * as fullcalendar from 'fullcalendar';
import { TimeGrid } from 'fullcalendar';

// TimeGrid and fullcalendar.views are used then

可以看到原始打字here https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/fullcalendar/index.d.ts.

fullcalendar-custom.d.ts 是

import * as FC from 'fullcalendar';

export as namespace FC;

declare class TimeGrid { prepareHits() }

declare let views: any;

这会导致类型错误,所以很明显fullcalendar命名空间未正确扩展:

TS2305:模块“.../node_modules/@types/fullcalendar/index”没有导出成员“TimeGrid”。

TS2339:类型“typeof”上不存在属性“views”.../node_modules/@types/ 全日历/索引"'。

这应该如何以正确的方式完成?

Can reference考虑到这里应该避免指令types目录指定于typeRoots?

该应用程序与 Webpack 和 Awesome-typescript-loader 捆绑在一起,因此其行为可能与其他编译方法不同。在某些时候,类型在 IDE 检查 (WebStorm) 中似乎没问题,但在编译时仍然出现类型错误。


我们可以在非声明中导入命名空间.ts,并将其再次导出为扩展类型:

// custom-fc.ts : enhances declaration of FC namespace
import * as origFC from "fullcalendar";

declare namespace Complimentary {
    class TimeGrid {
        prepareHits(): void;
    }
    let views: any;
}

// apply additional types to origFc and export again
export const FC: (typeof Complimentary & typeof origFC) = origFC as any;

 

// use-fc.ts : consumer of extended declaration
import { FC } from "./custom-fc";

console.log(FC.TimeGrid);
console.log(FC.views);

(这在某种程度上与您的场景有所不同,因为我正在使用@types/包和 webpackts-loader,但你应该能够做类似的事情。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从包类型扩展命名空间 的相关文章

随机推荐