Skip to content

Change from using "any" to "unknown" for unspecified types. #625

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

Conversation

rustyconover
Copy link

In accordance with Typescript guides the use of the "any" type doesn't
enforce many guarantees, it will match any type. In situations where
the type is unspecified it is better to use the "unknown" which is
closely related to the "any" type but requires a type assertion or
control flow based narrowing before the type can be used. The use of
the "unknown" type seems much more appropriate to use then "any" for
these cases.

See:

https://devblogs.microsoft.com/typescript/announcing-typescript-3-0-rc-2/#the-unknown-type

For more details.

In accordance with Typescript guides the use of the "any" type doesn't
enforce many guarantees, it will match any type.  In situations where
the type is unspecified it is better to use the "unknown" which is
closely related to the "any" type but requires a type assertion or
control flow based narrowing before the type can be used.  The use of
the "unknown" type seems much more appropriate to use then "any" for
these cases.

See:

https://devblogs.microsoft.com/typescript/announcing-typescript-3-0-rc-2/#the-unknown-type

For more details.
@codecov
Copy link

codecov bot commented Jun 3, 2021

Codecov Report

Merging #625 (0765348) into main (a36a728) will increase coverage by 1.71%.
The diff coverage is 88.53%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #625      +/-   ##
==========================================
+ Coverage   88.53%   90.25%   +1.71%     
==========================================
  Files           9       10       +1     
  Lines         349      390      +41     
  Branches      123      141      +18     
==========================================
+ Hits          309      352      +43     
- Misses         37       38       +1     
+ Partials        3        0       -3     
Impacted Files Coverage Δ
src/transform/headers.ts 23.07% <0.00%> (-4.20%) ⬇️
src/transform/index.ts 87.93% <78.78%> (+8.30%) ⬆️
src/transform/responses.ts 84.21% <81.81%> (-3.89%) ⬇️
src/transform/paths.ts 92.00% <87.50%> (ø)
src/transform/request.ts 95.00% <95.00%> (ø)
src/transform/parameters.ts 98.00% <96.42%> (+0.50%) ⬆️
src/index.ts 79.31% <100.00%> (+6.58%) ⬆️
src/transform/operation.ts 100.00% <100.00%> (+3.12%) ⬆️
src/transform/schema.ts 98.79% <100.00%> (+1.73%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e2fb371...0765348. Read the comment docs.

@drwpow drwpow mentioned this pull request Jun 3, 2021
Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m on board with this! Thank you for making this improvement.

Could you rebase off latest main and re-push? Once the conflicts are resolved I can merge.

@drwpow
Copy link
Contributor

drwpow commented Jun 13, 2021

I’m actually starting to second-guess this PR. Though I do think unknown may be a technically-better choice, it would be disruptive for people and I don’t think for most people there’s a clear benefit. I’d be OK in revisiting this as a breaking change later down the line, but perhaps we delay on this for now.

@rustyconover
Copy link
Author

Hi @drwpow,

I appreciate your reluctance to "break" things for users. While this change is increasing the safety of the generated interfaces it doesn't really do anything "wrong". If people are already relying on the behavior of "any" in their code, they're at risk right now of getting a type they don't actually expect. With "unknown", they need to be explicit with their types. This goes back to the Robustness Principal.

This change matters more for implementers of OpenAPI interfaces compared to clients. If you're sending data the use of "any" is no big deal because it is not your responsibility to handle the data (you're merely sending it on), but if you're the server/implementor of the OpenAPI interface it matters quite a bit.

It doesn't seem to me that many people use openapi-typescript for the server implementation side of things, and since I'm doing that I'm running into these issues.

Rusty

@dnalborczyk
Copy link
Contributor

@rustyconover generally I'm in favor of using unknown instead of any as well. I agree with @drwpow that this change could cause some breakage for some users of this project, although I haven't discovered any usage of any types in my own schema yet.

to get around this issue you could put this functionality behind a flag, which can then be removed with the next major bump.

@salemhilal
Copy link

I'd like to voice my support for this PR as well. I'm in the process of investigating writing a custom formatter to remove any from the generated types. For us, any shows up in instances when the endpoint we are typing doesn't have sufficient type information. It's caused a bunch of type-related errors in our codebase since it isn't always obvious that a type you are working with is actually any. Putting it behind a flag seems like a decent compromise, given that other levels of strictness are put behind flags as well (like --immutable-types).

@drwpow
Copy link
Contributor

drwpow commented Sep 30, 2021

Sorry for the delay on this—I had not had time the past few months to properly maintain the project. I will make this change in #769 and it will go out for the next release! And though this PR won’t be merged I’d still like to attribute credit for this code-writing and improvement.

@drwpow drwpow closed this Sep 30, 2021
@drwpow
Copy link
Contributor

drwpow commented Sep 30, 2021

@all-contributors please add @rustyconover for code

@allcontributors
Copy link
Contributor

@drwpow

I've put up a pull request to add @rustyconover! 🎉

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 this pull request may close these issues.

4 participants