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

DayJS import with typescript is failing (TS2349) #3572

Open
lcssanches opened this issue Jul 27, 2021 · 4 comments · May be fixed by #4813
Open

DayJS import with typescript is failing (TS2349) #3572

lcssanches opened this issue Jul 27, 2021 · 4 comments · May be fixed by #4813

Comments

@lcssanches
Copy link

Hello team,

I've configure NSwag to generate date as DayJS, but typescript is not compiling with * as dayjs.

This is the compilation failure that is happening:

This expression is not callable.
  Type 'typeof import("/csproj/ClientApp/node_modules/dayjs/index.d.ts")' has no call signatures.  TS2349

  > 982 | this.createdAt = _data["createdAt"] ? dayjs(_data["createdAt"].toString()) : <any>undefined;

This happens because the import is done like this

import * as dayjs from 'dayjs';

It works fine when the import statement is manually changed to:

import dayjs from 'dayjs';

Did I miss anything?

This is my nswag.json

{
  "runtime": "NetCore31",
  "defaultVariables": null,
  "documentGenerator": {
    "aspNetCoreToOpenApi": {
      "project": "Project.csproj",
      "msBuildProjectExtensionsPath": null,
      "configuration": null,
      "runtime": "",
      "targetFramework": "",
      "noBuild": true,
      "msBuildOutputPath": null,
      "verbose": true,
      "workingDirectory": null,
      "requireParametersWithoutDefault": false,
      "apiGroupNames": null,
      "defaultPropertyNameHandling": "Default",
      "defaultReferenceTypeNullHandling": "Null",
      "defaultDictionaryValueReferenceTypeNullHandling": "NotNull",
      "defaultResponseReferenceTypeNullHandling": "NotNull",
      "defaultEnumHandling": "Integer",
      "flattenInheritanceHierarchy": false,
      "generateKnownTypes": true,
      "generateEnumMappingDescription": false,
      "generateXmlObjects": false,
      "generateAbstractProperties": false,
      "generateAbstractSchemas": true,
      "ignoreObsoleteProperties": false,
      "allowReferencesWithProperties": false,
      "excludedTypeNames": [],
      "serviceHost": null,
      "serviceBasePath": null,
      "serviceSchemes": [],
      "infoTitle": "My Title",
      "infoDescription": null,
      "infoVersion": "1.0.0",
      "documentTemplate": null,
      "documentProcessorTypes": [],
      "operationProcessorTypes": [],
      "typeNameGeneratorType": null,
      "schemaNameGeneratorType": null,
      "contractResolverType": null,
      "serializerSettingsType": null,
      "useDocumentProvider": true,
      "documentName": "v1",
      "aspNetCoreEnvironment": null,
      "createWebHostBuilderMethod": null,
      "startupType": null,
      "allowNullableBodyParameters": true,
      "output": null,
      "outputType": "Swagger2",
      "newLineBehavior": "Auto",
      "assemblyPaths": [],
      "assemblyConfig": null,
      "referencePaths": [],
      "useNuGetCache": false
    }
  },
  "codeGenerators": {
    "openApiToTypeScriptClient": {
      "className": "{controller}Service",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.7,
      "template": "Axios",
      "promiseType": "Promise",
      "httpClass": "HttpClient",
      "withCredentials": false,
      "useSingletonProvider": false,
      "injectionTokenType": "OpaqueToken",
      "rxJsVersion": 6.0,
      "dateTimeType": "DayJS",
      "nullValue": "Undefined",
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "exportTypes": true,
      "wrapDtoExceptions": false,
      "exceptionClass": "ApiException",
      "clientBaseClass": null,
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "protectedMethods": [],
      "configurationClass": null,
      "useTransformOptionsMethod": false,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "markOptionalProperties": false,
      "generateCloneMethod": false,
      "typeStyle": "Class",
      "enumStyle": "Enum",
      "useLeafType": false,
      "classTypes": [],
      "extendedClasses": [],
      "extensionCode": null,
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "excludedParameterNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": false,
      "baseUrlTokenName": "API_BASE_URL",
      "queryNullValue": "",
      "useAbortSignal": false,
      "inlineNamedDictionaries": false,
      "inlineNamedAny": false,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "ClientApp/src/generated/Api.ts",
      "newLineBehavior": "Auto"
    }
  }
}
@MikeBecatti
Copy link

I had the same issue and resolved it by modifying the typescript template file. I modified line 70 of File.liquid to the import that works for the application that is consuming my client.

@RicoSuter
Copy link
Owner

@MikeBecatti can you create a PR with a change to this file so it works out-of-the-box?

@MikeBecatti
Copy link

@MikeBecatti can you create a PR with a change to this file so it works out-of-the-box?

@RicoSuter - Sorry but I should have added more information to my "solution". The change I recommended is really a work around for this issue rather than a true fix. Changing the import statement as the OP mentioned will cause compilation errors in other scenarios. I'll see if I can come up with a true solution.

@luizfernando1996
Copy link

Alternative solution.

Set this in nswag.json

   "importRequiredTypes": false,

and create a file (nswag.import.ts) with types import like that

import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelToken } from 'axios';
import dayjs from 'dayjs'

After this just add this propery in nswag.json

      "extensionCode": "nswag.import.ts",

@Seveilith Seveilith linked a pull request Mar 12, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

4 participants