question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript compilation error when using momentjs and TypeScript 3

See original GitHub issue

Using Typescript 3.* and momentjs for dates I get compile time errors.

Calls like moment() generate following error:

Cannot invoke an expression whose type lacks a call signature. Type ‘typeof moment’ has no compatible call signatures.ts(2349) api.ts(9, 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.

replacing moment() calls with moment.default() resolves the issue.

Im using following nswag.json

{ “runtime”: “NetCore21”, “defaultVariables”: null, “swaggerGenerator”: { “fromSwagger”: { “url”: “http://localhost:54408/swagger/v1/swagger.json”, “output”: null } }, “codeGenerators”: { “swaggerToTypeScriptClient”: { “className”: “{controller}Client”, “moduleName”: “”, “namespace”: “”, “typeScriptVersion”: 3.2, “template”: “Fetch”, “promiseType”: “Promise”, “httpClass”: “HttpClient”, “useSingletonProvider”: false, “injectionTokenType”: “OpaqueToken”, “rxJsVersion”: 6.0, “dateTimeType”: “momentjs”, “nullValue”: “Undefined”, “generateClientClasses”: true, “generateClientInterfaces”: false, “generateOptionalParameters”: false, “exportTypes”: true, “wrapDtoExceptions”: false, “clientBaseClass”: null, “wrapResponses”: false, “wrapResponseMethods”: [], “generateResponseClasses”: true, “responseClass”: “SwaggerResponse”, “protectedMethods”: [], “configurationClass”: null, “useTransformOptionsMethod”: false, “useTransformResultMethod”: false, “generateDtoTypes”: true, “operationGenerationMode”: “MultipleClientsFromOperationId”, “markOptionalProperties”: true, “generateCloneMethod”: false, “typeStyle”: “Class”, “classTypes”: [], “extendedClasses”: [], “extensionCode”: null, “generateDefaultValues”: true, “excludedTypeNames”: [], “handleReferences”: false, “generateConstructorInterface”: true, “convertConstructorInterfaceData”: false, “importRequiredTypes”: true, “useGetBaseUrlMethod”: false, “baseUrlTokenName”: “API_BASE_URL”, “queryNullValue”: “”, “inlineNamedDictionaries”: false, “templateDirectory”: null, “typeNameGeneratorType”: null, “propertyNameGeneratorType”: null, “enumNameGeneratorType”: null, “serviceHost”: “.”, “serviceSchemes”: null, “output”: “src/generated/api.ts” } } }

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:15
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

146reactions
jannikbuschkecommented, Jan 1, 2019

Actually the currently generated code seems not to be compliant with ES6. The import statement currently used (import * as moment from "moment") is a module import, and modules are not allowed to be called (i.e. moment() is forbidden). All named exports of moment can be accesed through the importet object. The default export can be called via moment.default() (as far as I understand it) ref1 https://stackoverflow.com/a/35706271/2017490 ref2 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

In my opinion the correct solution is to change import * as moment from "moment" to import moment from "moment". So we just import the default export and name it moment.

13reactions
p-obcommented, Jan 9, 2019

I’m seeing this issue as well (targeting TypeScript 3.2.2 in my application, NSwag is told to use 2.7 as that is the latest option available). I don’t know about any backwards compatibility issues that this change might cause, but simply changing import * as moment from 'moment'; to import moment from 'moment'; makes the errors further down with e.g. moment(data["started"].toString()) go away.

I’m using moment version 2.23.0 FYI

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot invoke an expression whose type lacks a call ...
This happens when Typescript compiler does not find the type definition of the methods you are using. You need to install @types/moment as...
Read more >
Moment.js using with Typescript
In this article, we will learn how to use the Moment.js library with Typescript language. Typescript is a strongly-typed programming ...
Read more >
TypeScript vs. JavaScript: Your Go-to Guide
TypeScript runs its type checks while transpiling—a form of compiling that converts TypeScript code to the JavaScript code web browsers and Node.js understand....
Read more >
Moment.js | Docs
As of version 2.13.0, Moment includes a typescript definition file. Install via NPM npm install moment. Import and use in your Typescript file...
Read more >
Errors when using MomentJS in Angular Typescript library ...
import moment from 'moment';. Firstly to make this work, you need to specify the --allowSyntheticDefaultImports flag. tsconfig.json { "compilerOptions": { " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found