Skip to content

Commit

Permalink
Added check on SupportsTypeOnlyImports when TypeScript version is >= …
Browse files Browse the repository at this point in the history
…4.3 and updated File.liquid template for TypeScript (#4005)

* - The TypeScript compiler type checking produces an error for the import of the axios types
- Not possible to change the preserveValueImports or isolatedModules compiler options because they are needed by vite-vue
- also see Issue 3964 - type-only imports for axios (#3964)
- Changed axios imports to support import type when typescript version >= 4.3 by adding SupportsTypeOnlyImports in TypeScriptFileTemplateModel.cs
- It is supported as from TypeScript version 3.8 but only versions 2.7, 4.3, so picked 4.3 (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html)

* changed to TypeScript 3.8 as its supported from this version (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html)
You can't set it in the UI but user can define free value in JSON configuration.

Co-authored-by: Tom van de Kerkhof <tom.vandekerkhof2@kbc.be>
  • Loading branch information
tomvdk and tvandekerkhof authored May 18, 2022
1 parent c928ef8 commit 4749251
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public IEnumerable<string> ResponseClassNames
/// <summary>Gets a value indicating whether the target TypeScript version supports override keyword.</summary>
public bool SupportsOverrideKeyword => _settings.TypeScriptGeneratorSettings.SupportsOverrideKeyword;

/// <summary>Gets a value indicating whether the target TypeScript version supports Type-Only imports</summary>
public bool SupportsTypeOnlyImports => _settings.TypeScriptGeneratorSettings.TypeScriptVersion >= 3.8m;

private string GenerateExtensionCodeAfter()
{
var clientClassesVariable = "{" + string.Join(", ", _clientTypes
Expand Down
6 changes: 5 additions & 1 deletion src/NSwag.CodeGeneration.TypeScript/Templates/File.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ import { HttpClient, RequestInit } from 'aurelia-fetch-client';
import * as ng from 'angular';
{%- endif -%}
{%- if Framework.IsAxios -%}

{%- if SupportsTypeOnlyImports -%}
import axios, { AxiosError } from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelToken } from 'axios';
{%- else -%}
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelToken } from 'axios';
{%- endif -%}
{%- endif -%}
{%- if Framework.IsKnockout -%}

Expand Down

0 comments on commit 4749251

Please sign in to comment.