打字稿,库:react-time-ago认为它需要一个数字,即给它一个字符串。我不知道如何接受数字?
import ReactTimeAgo from "react-time-ago"
<ReactTimeAgo
date = {tweet._createdAt}
/>
_CreatEdat将时间输出为2022-06-02T01:16:40Z
我正在使用React-time-ago将其更改为21分钟前。它实际上可以正常工作。但是,打字稿抱怨停留该日期参数需要作为一个数字来。因此,我进入了index.d.ts,然后将类型从数字更改为任何。
interface Props extends React.HTMLAttributes<HTMLElement> {
date: Date | any;
future?: boolean;
timeStyle?: string | Style;
round?: string;
minTimeLeft?: number;
tooltip?: boolean;
component?: React.ReactType;
wrapperComponent?: React.ReactType;
wrapperProps?: object;
locale?: string;
locales?: string[];
formatVerboseDate?: (date: Date) => string;
verboseDateFormat?: object;
这阻止了VSCODE的错误,但是由于该错误,它仍然显示在浏览器上,而我的部署失败了。我如何获得打字稿以停止抱怨获得字符串?
- 它应该得到该字符串,如果我给它一个数字,它给出了不正确的值。如果我使用 @ts-ignore或 @ts-nocheck,我仍然会出现错误。似乎对我无能为力。感谢您的帮助!
import ReactTimeAgo from "react-time-ago"
<ReactTimeAgo
date = {tweet._createdAt}
/>
_createdAt outputs time as 2022-06-02T01:16:40Z
I'm using React-time-ago to change it into like 21 minutes ago. It actually works fine. Yet, TypeScript complains staying that date parameter needs to come in as a number. so i went into the index.d.ts and changed the type from number to any.
interface Props extends React.HTMLAttributes<HTMLElement> {
date: Date | any;
future?: boolean;
timeStyle?: string | Style;
round?: string;
minTimeLeft?: number;
tooltip?: boolean;
component?: React.ReactType;
wrapperComponent?: React.ReactType;
wrapperProps?: object;
locale?: string;
locales?: string[];
formatVerboseDate?: (date: Date) => string;
verboseDateFormat?: object;
That stopped the error for vscode, but it still shows up on the browser and my deployment fails because of that error. How do I get typescript to stop complaining about getting a string?
- it should be getting that string, if I give it a number, it gives the incorrect value. and I still get the error if I use @ts-ignore or @ts-nocheck. Seems to do nothing for me. Thanks for any help!
This is the error
https://i.sstatic.net/b85pA.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不应该尝试手动更改库文件。
date
此软件包中的属性接受的联合类型
号码。因此,最好的选择是在喂食date
属性字段之前,将日期字符串转换为date
对象。您可以使用类似的方法;将该日期字符串解析到日期对象中,然后将其作为道具。
通过编辑您的直接示例;
First of all, you shouldn't try to change the library files manually.
The
date
property in this package accepts a union type ofDate
andnumber
. So your best bet would be to convert the date string into aDate
object before feeding thedate
property field. You can use a method like;To parse that date string into a date object and then give it as a prop.
By editing your direct example;