Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeScript, Library:React-Time-Ago thinks it requires a number, I'm giving it a string. I cant figure out how to make it accept a number?

       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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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://imgur.com/YjSt816

>Solution :

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)}
/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading