Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript error with extendMoment() #263

Open
nareshbhatia opened this issue Jun 7, 2019 · 7 comments
Open

TypeScript error with extendMoment() #263

nareshbhatia opened this issue Jun 7, 2019 · 7 comments

Comments

@nareshbhatia
Copy link

Following the ES6 installation instructions to use moment-range with TypeScript 3.4.5 I was getting the following error on extendMoment():

Argument of type 'typeof import("/Users/narbhati/projects/slidesup/node_modules/moment/moment.d.ts")' is not assignable to parameter of type 'typeof import("/Users/narbhati/projects/slidesup/node_modules/moment/moment.d.ts")'.
Property 'default' is missing in type 'typeof import("/Users/narbhati/projects/slidesup/node_modules/moment/moment.d.ts")' but required in type 'typeof import("/Users/narbhati/projects/slidesup/node_modules/moment/moment.d.ts")'. TS2345

I had to change the moment import statement as follows to get rid of this error:

// import Moment from 'moment';
import * as Moment from 'moment';
import { extendMoment } from 'moment-range';

const moment = extendMoment(Moment);

Surprisingly, the original import statement works perfectly fine in other files that do not use moment-range (I use esModuleInterop: true in tsconfig).

For example, this works fine:

import moment from 'moment';
import 'moment-timezone';
import 'moment-duration-format';

Would be good to document the correct way to use moment-range with TypeScript.

@ddehghan
Copy link

ddehghan commented Oct 4, 2019

They only way I got this to work in "typescript": "3.4.5"

const Moment = require('moment');
import {extendMoment} from 'moment-range';

const moment = extendMoment(Moment);

@TristanJM
Copy link
Contributor

Thanks @nareshbhatia! I've updated the docs to reflect that fix :)
Does this also work for your case @ddehghan?

@nareshbhatia
Copy link
Author

nareshbhatia commented Oct 4, 2019

@TristanJM, it's a workaround at best :-). TypeScript is getting very popular in the React circles now, so finding a natural solution would be good.

@ddehghan
Copy link

ddehghan commented Oct 4, 2019

Sadly above also failed and gave me lint errors. I fixed it like this.

import * as moment from 'moment';
import {extendMoment} from 'moment-range';

const {range} = extendMoment(moment);

const r = range(moment.utc('2019-10-01T00:00:00.000Z'), now);

but even this latest iteration has a problem where calling moment default functions causes this error.

const xxx = moment('2019-10-01T00:00:00.000Z');

This expression is not callable.
  Type 'typeof import(".../node_modules/moment/moment.d.ts")' has no call signatures.ts(2349)
subscription.ts(15, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.

@minamorl
Copy link

minamorl commented Jan 20, 2020

This workaround works for me:

// This line has changed. I think "* as Moment" is wrong.
import Moment from "moment"; 

import { extendMoment } from "moment-range";

// Current latest release has wrong type definition,
// so I put any casting to avoid errors.
const moment = extendMoment(Moment as any);

@Mach12
Copy link

Mach12 commented Apr 8, 2022

I've come across this problem and came up with the following:

import * as Moment from 'moment'
import { extendMoment } from 'moment-range'

const { default: moment, range: momentRange } = extendMoment(Moment)

I couldn't find an approach that allows to write moment.range without going through any, but this seems to work in TS 4.4 and neither TS nor our linter complains.

@stefanlogue
Copy link

I've come across this problem and came up with the following:

import * as Moment from 'moment'
import { extendMoment } from 'moment-range'

const { default: moment, range: momentRange } = extendMoment(Moment)

I couldn't find an approach that allows to write moment.range without going through any, but this seems to work in TS 4.4 and neither TS nor our linter complains.

Just spent a day trying to figure out why intersect didn't exist on my range, this fixed it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants