如果我的站点已经在使用moment.min.js,如何使用Moment.js TypeScript定义文件? [英] How to consume the Moment.js TypeScript definition file if my site is already using moment.min.js?

查看:410
本文介绍了如果我的站点已经在使用moment.min.js,如何使用Moment.js TypeScript定义文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换一个网站以使用TypeScript,而我只是将许多JavaScript文件之一转换为TypeScript. 我网站上的所有页面都已经引用了moment.js,例如:

I'm in the process of converting a website to use TypeScript and I'm converting just one of many JavaScript files to TypeScript. All pages of my site already reference moment.js, such as:

<script src="/scripts/moment.min.js"></script>

我使用以下命令添加了其他TypeScript定义文件:

I've added other TypeScript definition files using:

npm install --save-dev @types/jquery

...但是,以上似乎暂时是错误的选择.当我使用上述命令(但用"moment"代替"jquery")时,将下载一个自述文件,内容为:

... But, the above seems like the wrong choice for moment. When I use the above command (but substitute 'moment' for 'jquery'), a readme file is downloaded that says:

这是Moment的存根类型定义( https://github.com/moment/moment ). Moment提供了自己的类型定义,因此您不需要安装@ types/moment!

This is a stub types definition for Moment (https://github.com/moment/moment). Moment provides its own type definitions, so you don't need @types/moment installed!

作为一种解决方案,我尝试从其GitHub存储库中保存moment.d.ts文件,并按如下方式在TypeScript文件中对其进行引用:

As a solution, I tried saving the moment.d.ts file from its GitHub repo and referencing it in the TypeScript file as so:

///<reference path="../../Scripts/typeDeclarations/moment.d.ts"/>
var now:any = moment.utc();

但是,TypeScript给我警告:

But, TypeScript gives me the warning:

找不到名称"moment"

Cannot find the name 'moment'

推荐答案

一种方法是创建自定义类型,例如custom-typings/moment.d.ts:

One way to do this is create a custom typing, e.g. custom-typings/moment.d.ts:

export * from 'moment'
export as namespace moment

并将该文件夹包含在您的tsconfig.json中:

And include that folder in your tsconfig.json:

{
  include: [
    "custom-typings"
  ]
}

这篇关于如果我的站点已经在使用moment.min.js,如何使用Moment.js TypeScript定义文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆