打字稿,库:react-time-ago认为它需要一个数字,即给它一个字符串。我不知道如何接受数字?

发布于 2025-02-04 14:05:56 字数 1089 浏览 2 评论 0原文

       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,我仍然会出现错误。似乎对我无能为力。感谢您的帮助!

这是错误 https://i.sstatic.net/b85pa.jpg

       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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紙鸢 2025-02-11 14:05:56

首先,您不应该尝试手动更改库文件。

date此软件包中的属性接受的联合类型 号码。因此,最好的选择是在喂食date属性字段之前,将日期字符串转换为date对象。您可以使用类似的方法;

const creationDate: Date = new Date('2022-06-02T01:16:40Z')

将该日期字符串解析到日期对象中,然后将其作为道具。

通过编辑您的直接示例;

import ReactTimeAgo from "react-time-ago"

<ReactTimeAgo 
  date = {new Date(tweet._createdAt)}
/>

First of all, you shouldn't try to change the library files manually.

The date property in this package accepts a union type of Date and number. So your best bet would be to convert the date string into a Date object before feeding the date property field. You can use a method like;

const creationDate: Date = new Date('2022-06-02T01:16:40Z')

To parse that date string into a date object and then give it as a prop.

By editing your direct example;

import ReactTimeAgo from "react-time-ago"

<ReactTimeAgo 
  date = {new Date(tweet._createdAt)}
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文