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
Discuss changes for TypeScript strict checks #638
Comments
@RSuter Based on the above, could you take a look at master...grovesNL:typescript-type-improvements when you have some time and let me know your thoughts? I can open a PR but I consider it experimental for now. |
The return type must match the generated method signature (ie the signature wins). |
@RSuter But in some cases the signature that was used was impossible. i.e. Likewise because this return type is used in |
The whole strict null thing is not yet implemented anywhere... We need to fix the method headers (with "| null" as you suggested) and also fix the "return null;" if it is wrong. But it looks like the method signature is wrong in this case... The we need to add "| null" to the return types which are nullable but only if strict null is supported (TS >= v2.0?) I think this must be done in NJsonSchema |
@RSuter Yep exactly, that's what I'm attempting in https://github.com/grovesNL/NSwag/tree/typescript-type-improvements (you can see all the added There are some changes that have to happen in NJsonSchema for the conversion code but the majority will have to happen on the NSwag side. If you look at my commit messages in the referenced URL, I have the majority of the strict options (like strictNullCheck) working with these updated return types/other trivial changes. There are only a few errors that remain. |
@RSuter Let me know whether you think it's worthwhile to make these changes. If you're interested, I'll try to merge the recent template changes from master so my branch doesn't fall too far behind |
NJS also updated for strict null checks: RicoSuter/NJsonSchema@a561d72 |
I think your changes are worth it. But we have to be very careful (no breaking changes, support for TS 1.8). I've seen Can you create a list of the changes? |
@RSuter Sounds good. For the As I mentioned above, there are some places where null is returned explicitly. In this case the return type for the caller may look like My changes should be documented with each commit message but I can add the list here as well. I also need to add flags to support TS 1.8 wherever |
Also great job adding support for strict null checks to NJS. After we add the changes I mentioned here, we should only have a couple "strict options" issues left. |
For the given example: The problem is, that we don't know whether the response is nullable or not. In the Swagger spec, there is no means of specifying if "null" or "undefined" is allowed (the JSON Schema type "null" is not allowed and there is no property "required" or "optional"). NSwag generates the property "x-nullable" for that which can be evaluated with the The question: Should we support "undefined" as response? I'd say no, then we should use the following rules:
|
With response: Without response: |
Maybe we should use
Meaning:
This way the generation is simpler (you don't have to check whether to generate the default What do you think? |
I think those signatures are fine from the caller's perspective. It's obvious from the return type what actually happened if you want to handle Although I'm not sure if the If we want to support For the note about |
@RSuter I updated my branch slightly and merged in your latest changes. The diff is relatively clean but the changes are basically:
With these changes, almost all of the templates satisfy the strict checks. You can compile the TS clients with the new release mode (ReleaseTypeScriptStrict) to see where the remaining errors occur. The remaining errors should only occur in
We could do a simple string match or similar here to check if Let me know what you think. I'll send a PR if you're fine with the changes. |
This looks good to me... can you create a PR for the latest master commit? |
Minor change request: Rename
to
|
What exactly does that mean? I think we may break backward compatibility here... We should add an option for that (default: generate imports) so that the old behavior can be enabled. Next release will be major (v10) because there are some small breaking changes... I hope to merge your work before releasing. |
Sounds good, thanks for reviewing. The new TypeScript type definitions provided by @types modules are not ambient, so you have to import each module's types whenever you need them. So we have to manually import Angular and Knockout types. The @types method is the preferred way of managing types now but you're right, it would likely cause issues if the older tsd etc. methods are used. I'm traveling today but when I have some time (probably tomorrow) I'll rename the property, add the import option to preserve backwards compatibility and send a PR. |
Should we call it |
I think we also need to import JQuery:
for the JQuery templates |
Just FYI: NJsonSchema and NSwag are tightly coupled... we can work there as well. I'm the developer of both libraries... |
@RSuter Yeah I knew that, thanks. Hopefully we shouldn't need to make too many changes there for now. The jQuery import doesn't seem to be required by now due to the static declaration but we should probably import it to be explicit anyway (in case the definition changes to only export the module). On closer inspection Knockout seems to have a static declaration too, but we may as well keep the import to be explicit. I think |
See #497 |
@RSuter Seems like we'll need another option besides |
Ok, now I've got only this error:
|
@RSuter: Looks like it's this issue: angular/angular#8720 Which version of tsc and @angular/core are installed in node_modules? If you delete the @angular/core folder in node_modules and do Thanks for adding the explicit return types back for those methods. |
I also added the MomentJS import... The only problem now is that fetch/aurelia do not have Access to the Response if the default Response is blob.. |
@RSuter: How is it different than methods that return other objects? i.e. https://github.com/NSwag/NSwag/blob/master/src/NSwag.Integration.TypeScriptWeb/scripts/serviceClientsFetch.ts#L415 Where would the caller be able to access the |
You cannot access the response object from outside... |
The problem is this:
As you can see, in the exception case, the responseText is "" (we have to completely change the templates to support that) |
Oh, you mean when it's passed to I suppose we could check the default status before we call Alternatively we could probably parse the A third option is to clone the stream earlier and use that, but that's not very efficient. |
I think the best option is a) and read blob or text inside the "if status code", but this will require some changes... Blob to text is not simple and clone is to expensive |
I will look into this today or tomorrow... Otherwise i think we are good? Are you doing some integration tests with the latest bits? Do you think we need an option to disable blob handling and use the previous (any) behavior? |
I haven't done any integration tests yet but I'll try to do this soon if I have some time. Functionally I don't think anything should have changed so it should be fine. I'm not using blobs so that change wouldn't affect me. |
Everything discussed in this issue has been resolved as far as I know - closing. |
Just wanted a place to document and discuss any changes we consider in the TypeScript clients as we add stricter checks in case the context behind the original types were missed.
Firstly consider the
return null;
within a method expecting a return type ofvoid
. Do we intentionally return null here (in which case we should update the return type) or could this statement simply bereturn;
instead? If this could be clarified I'd be happy to make the change, but I want to avoid breaking changes ifnull
is actually expected.I found where
return null;
was added to try to gain some context. It was added in0edf241#diff-1e10b65d13165f5da1c352bfcbceba19 but I can't tell whether this is the desired behavior.
The text was updated successfully, but these errors were encountered: