Typescript compilation error when using momentjs and TypeScript 3
See original GitHub issueUsing 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:
- Created 5 years ago
- Reactions:15
- Comments:24 (10 by maintainers)
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 viamoment.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/importIn my opinion the correct solution is to change
import * as moment from "moment"
toimport moment from "moment"
. So we just import the default export and name it moment.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';
toimport 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