简体   繁体   English

如何将 date-fns 与 TypeScript 一起使用?

[英]How do I use date-fns with TypeScript?

I have an ASP.NET Core website with TypeScript.我有一个带有 TypeScript 的 ASP.NET 核心网站。 My ASP.NET Core setup is typical in that it has a node_modules folder in the root of the project and then I copy the source folders of the libraries I'm using from node_modules to wwwroot/lib using Gulp.我的 ASP.NET 核心设置很典型,因为它在项目的根目录中有一个node_modules文件夹,然后我使用 Gulp 将我正在使用的库的源文件夹从node_moduleswwwroot/lib

Therefore, I have date-fns source files in wwwroot/lib/date-fns and in my TypeScript .ts file I'm trying to import it like this:因此,我在wwwroot/lib/date-fns fns 和 TypeScript .ts文件中有 date-fns 源文件,我试图像这样导入它:

import * as fns from "lib/date-fns/i.am.stuck.here.js";

The VS2019 code editor is autocompleting the folder so it can see it, but I don't know what .js file to enter, it doesn't suggest one. VS2019 代码编辑器正在自动补全文件夹,所以它可以看到它,但我不知道要输入什么.js文件,它不建议一个。

I also don't know if I should import * or something else.我也不知道我是否应该导入 * 或其他东西。 I'm not a frontend dev so I struggle with TS and JS and imports and requires and all that stuff.我不是前端开发人员,所以我与 TS 和 JS 以及导入和要求以及所有这些东西斗争。

I can see that date-fns has a typings file so I assume this is straight forward for those in the know.我可以看到 date-fns 有一个类型文件,所以我认为这对于那些知道的人来说是直截了当的。


Update:更新:

I found this:我找到了这个:

https://github.com/date-fns/date-fns/blob/master/examples/typescript/example.ts https://github.com/date-fns/date-fns/blob/master/examples/typescript/example.ts

Which is strange because updating my code to the below, which is auto-suggested as the editor reads the folders on disk:这很奇怪,因为将我的代码更新为以下内容,这是在编辑器读取磁盘上的文件夹时自动建议的:

import { format } from "lib/date-fns";

Leaves me with an error:给我一个错误:

error TS2307: Cannot find module 'lib/date-fns' or its corresponding type declarations.

When I was importing moment.js I pointed it at the .js file itself, which was auto-suggested.当我导入 moment.js 时,我将它指向.js文件本身,这是自动建议的。

Also, if I change to 'lib/date-not-exist' I get the same error, so I'm unable to tell if it's a file path existence issue or a TypeScript locating module issue.此外,如果我更改为'lib/date-not-exist'我会收到同样的错误,所以我无法判断这是文件路径存在问题还是 TypeScript 定位模块问题。


Update:更新:

I can get TypeScript to squelch the error and presumably find a module with this:我可以得到 TypeScript 来消除错误并大概找到一个模块:

import * as dformat from "lib/date-fns/format/index";

But this doesn't work because:但这不起作用,因为:

var result = dformat(date, 'dd.MM.yyy HH:mm:ss');

Gives error:给出错误:

error TS2349: This expression is not callable.

See my answer in Import a JavaScript module or library into TypeScript if you're having problems importing modules generally using TypeScript and JavaScript in the browser. See my answer in Import a JavaScript module or library into TypeScript if you're having problems importing modules generally using TypeScript and JavaScript in the browser.

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

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