You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
4.1.1 seems to have the same problem, but less serious because it at least compiles (with warnings).
However the client code has to treat the value as a "List" because it has this interface:
exportinterfaceList{/** * * @type {number} * @memberof List */start?: number;/** * * @type {Array<object>} * @memberof List */hits?: Array<object>;/** * * @type {number} * @memberof List */total?: number;/** * * @type {number} * @memberof List */size?: number;}
It is clearly not an Array, it does not even have a length property. ListFromJSON returns a List because that is what the JSON structure passed from the server its, not an array.
I don't really understand why TypeScript allows this, it seems to be a limitation in the type checking, effectively it allows:
Promise<List> to be silently cast to Promise<Array> and then Promise<Array> to be cast back to Promise<List> in the client code.
The type really is not an Array at all. My code works now without hand editing, so I am not worried about this, but you should be worried about this because a type is completely wrong and all it would take would be one dereference to crash at runtime. Is there a deeper problem here?
Have just discovered this is an Error in production mode, it's a warning only for development compilation, so it is important to me to get this resolved.
[ ERROR ] TypeScript: src/kaizen-api/src/apis/EventsApi.ts:182:102
Generic type 'Array<T>' requires 1 type argument(s).
L181: */
L182: async postEventSearchRaw(requestParameters: PostEventSearchRequest): Promise<runtime.ApiResponse<Array>> {
L183: if (requestParameters.payload === null || requestParameters.payload === undefined) {
[ ERROR ] TypeScript: src/kaizen-api/src/apis/UsersApi.ts:390:100
Generic type 'Array<T>' requires 1 type argument(s).
L389: */
L390: async postUserSearchRaw(requestParameters: PostUserSearchRequest): Promise<runtime.ApiResponse<Array>> {
L391: if (requestParameters.payload === null || requestParameters.payload === undefined) {
[ ERROR ] TypeScript: src/kaizen-api/src/runtime.ts:127:24
Cannot find name 'GlobalFetch'.
L127: export type FetchAPI = GlobalFetch['fetch'];
[22:56.4] build failed in 14.45 s
So I am back to hand editing the Array to List to get this to work.
So postEventSearch returns a single List object not an Array.
I am happy to look at implementing a fix, but I have not looked at the OpenAPI generator code before. Where is the code that derives what the return type should be? In the template it gets referred to as returnType so it is perhaps not a template problem? How is the returnType derived?
It defines that the type list maps to a (TypeScript) array which in my opinion doesn't make a whole lot of sense to include - The OAI Spec doesn't define that list is equivalent to array and this also doesn't make a whole lot of sense in the context of TypeScript. Removing that line will likely fix this bug.
Note that removing this will be a breaking change. However, I'd hope no one relies on this behavior.
@keean would you be willing to test this and submit a PR if this fixes the bug?
I am happy to test. As far as I am aware there is no generic List type in TypeScript, and it is not a reserved word in OpenAPI. List of TypeScript Basic types:
But whatever language is used, unless OpenAPI defines it as a reserved word, I would expect the schema custom definition to be used, which means in a language where "List" is reserved, then the OpenAPI type would need to be mapped to "ApiList" or some other identifier.
I would assume that an OpenAPI sphema referencing an undefined "List" object would fail to validate?
So if I comment out that line, it generates the correct types.
However I still have to make a manual change to get it to compile without error, that I might as well mention here, I have to replace GlobalFetch with WindowOrWorkerGlobalScope in src/runtime.ts
Reacted by Angelina Mohr, Juan Herrera and Brad Johnson
@Bakkiyaraj A general workaround is to replace GlobalFetch['fetch'] with WindowOrWorkerGlobalScope['fetch'].
The correct code is generated if you use the --additional-properties=typescriptThreePlus=true flag during generation
Reacted by vrnd.studio, Eno Compton, Matt Gregg, Kojiro Ishii, Christopher Fitzner, Honza, Gary Gurlaskie, Dexter Legaspi, Robert Gruner, Abhijith Reddy and 39 more
Reacted by Dexter Legaspi, Abhijith Reddy, Oliver, Mateja Petrovic, Rui Araújo, Majid Dadashi, Krzysztof Kabala, Kishan Reddy Alumalla, Marc, Adrien Horgnies and 5 more
@janniclas I'm confused: shouldn't TS3+ be the default?
Reacted by Kyrylo Yakymenko, pierre lepers, Oren Klein, Marc, Alex Lukashou, Charlie Brown, Kuba Jastrzębski, Andrea, Joey Mezzacappa, Daniel B and 5 more
I am experiencing the typescript error due to Array without generics with typescript-fetch using version 6.0.0, too - I don't have the items in OpenApi as it is a free-form input; I'd expect Array<any>. Can I do something about this in this version?
Activity
auto-labeler commentedon Sep 10, 2019
👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
macjohnny commentedon Sep 10, 2019
can you explain why it should be
Listinstead ofArray?keean commentedon Sep 10, 2019
First,
Arrayis not a good typescript type (at least not in TypeScript 3.1.3). This will generate the compile time warning:It should be
Array<T>and what isT? I guess the closest allowable typescript type would beArray<any>.The easiest way to see what type it should be is to look at the code. If we look at the return statement from the function starting at line 182:
We can see that the parameter to
runtime.ApiResponsemust be the same as the type returned fromListFromJSONwhich returnsList.macjohnny commentedon Sep 10, 2019
@keean can you try with the latest version
4.1.1?keean commentedon Sep 10, 2019
4.1.1 seems to have the same problem, but less serious because it at least compiles (with warnings).
However the client code has to treat the value as a "List" because it has this interface:
It is clearly not an
Array, it does not even have alengthproperty. ListFromJSON returns aListbecause that is what the JSON structure passed from the server its, not an array.I don't really understand why TypeScript allows this, it seems to be a limitation in the type checking, effectively it allows:
Promise<List>to be silently cast toPromise<Array>and thenPromise<Array>to be cast back toPromise<List>in the client code.The type really is not an Array at all. My code works now without hand editing, so I am not worried about this, but you should be worried about this because a type is completely wrong and all it would take would be one dereference to crash at runtime. Is there a deeper problem here?
keean commentedon Sep 10, 2019
Have just discovered this is an Error in production mode, it's a warning only for development compilation, so it is important to me to get this resolved.
So I am back to hand editing the
ArraytoListto get this to work.macjohnny commentedon Sep 10, 2019
Would you like to implement a fix?
I think the type should be Array, and hence fixed in the ...FromJSON
@TiFu what is your opinion?
keean commentedon Sep 10, 2019
If we add a console.log, we can see the actual type there is a
Listnot anArray:So we can clearly see the Promise resolves to a
Listand not anArray, hence the return type of postEventSearch must bePromise<List>The ListFromJSON is returning the correct type. You can clearly see what it is meant to be looking at the swagger.json file:
and the reference to
Listis clearly an object not an array:So postEventSearch returns a single List object not an Array.
I am happy to look at implementing a fix, but I have not looked at the OpenAPI generator code before. Where is the code that derives what the return type should be? In the template it gets referred to as
returnTypeso it is perhaps not a template problem? How is the returnType derived?TiFu commentedon Sep 11, 2019
@macjohnny I agree with @keean that it should be of type
List.I think the bug is in AbstractTypeScriptClientCodegen:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java#L110
It defines that the type
listmaps to a (TypeScript) array which in my opinion doesn't make a whole lot of sense to include - The OAI Spec doesn't define that list is equivalent to array and this also doesn't make a whole lot of sense in the context of TypeScript. Removing that line will likely fix this bug.Note that removing this will be a breaking change. However, I'd hope no one relies on this behavior.
@keean would you be willing to test this and submit a PR if this fixes the bug?
macjohnny commentedon Sep 11, 2019
@tifi @keean thanks for the analysis. Now I understand,
Listis a custom type, and I agree, the mapping inopenapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java
Line 110 in e73bf9b
does not make sense. @wing328 can you confirm there is no generic type
Listin the parsed spec?keean commentedon Sep 11, 2019
I am happy to test. As far as I am aware there is no generic
Listtype in TypeScript, and it is not a reserved word in OpenAPI. List of TypeScript Basic types:https://www.typescriptlang.org/docs/handbook/basic-types.html
And TypeScript advanced types:
https://www.typescriptlang.org/docs/handbook/advanced-types.html
But whatever language is used, unless OpenAPI defines it as a reserved word, I would expect the schema custom definition to be used, which means in a language where "List" is reserved, then the OpenAPI type would need to be mapped to "ApiList" or some other identifier.
I would assume that an OpenAPI sphema referencing an undefined "List" object would fail to validate?
wing328 commentedon Sep 11, 2019
"List" is not an OpenAPI type from what I can tell.
keean commentedon Sep 11, 2019
So if I comment out that line, it generates the correct types.
However I still have to make a manual change to get it to compile without error, that I might as well mention here, I have to replace GlobalFetch with WindowOrWorkerGlobalScope in src/runtime.ts
macjohnny commentedon Sep 11, 2019
An option to generate code for typescript 3+ was introduced, which should fix that
evstinik commentedon Nov 19, 2019
@macjohnny oh finally, you saved my life. An option for ts3+.. how long I've been searching for that just to switch to newest ts version 3.7. Thanks!
dngconsulting commentedon Dec 5, 2019
It it possible to fix this compilation error also ?
Thanx
Sami
macjohnny commentedon Dec 5, 2019
@dngconsulting yes, I would be happy to review your pull-request.
dngconsulting commentedon Dec 5, 2019
@macjohnny here we go => #4711 hoping that the merge will be as quick as your answer 😉
Bakkiyaraj commentedon Feb 6, 2020
Cannot find name 'GlobalFetch'. TS2304
export type FetchAPI = GlobalFetch['fetch'];
We are using typescript-fetch to generate models. Can you please confirm which release has the fix?
or suggest the workaround for this
janniclas commentedon Feb 10, 2020
@Bakkiyaraj A general workaround is to replace
GlobalFetch['fetch']withWindowOrWorkerGlobalScope['fetch'].The correct code is generated if you use the
--additional-properties=typescriptThreePlus=trueflag during generationtrumbitta commentedon Oct 30, 2020
@janniclas I'm confused: shouldn't TS3+ be the default?
DanielRuf commentedon Nov 18, 2021
Related: #9973
rvansa commentedon Jun 1, 2022
I am experiencing the typescript error due to
Arraywithout generics withtypescript-fetchusing version6.0.0, too - I don't have theitemsin OpenApi as it is a free-form input; I'd expectArray<any>. Can I do something about this in this version?