Home > Back-end >  Unable to use new Date() in Typescript - TS2554: Expected 1-2 arguments, but got 0
Unable to use new Date() in Typescript - TS2554: Expected 1-2 arguments, but got 0

Time:01-20

I am trying to use a js date with Ts and seemingly unable to .

Usage

<Date variant="body2">on {format(new Date(), 'dd/MM/yyyy')}</Date>

Error

Expected 1-2 arguments, but got 0.  TS2554

  > 162 |             <Date variant="body2">on {format(new Date(), 'dd/MM/yyyy')}</Date>
        |                                              ^
    163 |           </>
    164 |         )}
    165 |       </UploadBox>

enter image description here

CodePudding user response:

The issue is that you have imported a React component named Date into scope and shadowed the built-in Date object. Your code probably looks like this:

import { Date } from 'some/ui/library';

// ... snip ...
function MyCoolComponent(props) {
  return <Date>{format(...)}</Date>;
}

If you want access to the global Date object you need to either:

  • Import the component in another way (e. g. import { Date as DateElement } from 'some/ui/library';) or
  • Access the global Date object off of the global (using window or globalThis) - <Date>{format(new window.Date())}</Date>;
  •  Tags:  
  • Related