Skip to content

Use unknown instead of any #554

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

Closed
andrenarchy opened this issue Apr 6, 2021 · 4 comments
Closed

Use unknown instead of any #554

andrenarchy opened this issue Apr 6, 2021 · 4 comments

Comments

@andrenarchy
Copy link

It would be great if the resulting ts would not use any but unknown instead.

@drwpow
Copy link
Contributor

drwpow commented Apr 15, 2021

Thanks for the suggestion. Do you have an example of a schema you’re using, what it’s generating, and what you’d like it to be instead?

Also would you be open to opening a PR for this?

@andrenarchy
Copy link
Author

One example is a schema I have with the following property somewhere:

          "presets": {
            "description": "JSON structure containing the preset value for created/updated items.",
            "type": "object",
            "nullable": true
          },

This gets transformed into:

presets?: { [key: string]: any } | null

The use of any is generally discouraged since it can be accessed in any way. But that's not really what we want for a type where we don't have further knowledge. So the better option would be to use unknown for all types that are currently generated as any because then it's up to the user to do further checks on the type. In the above example I'd expect:

presets?: { [key: string]: unknown } | null

See also eslint (which is complaining about any in the recommended rule set) and the TypeScript announcement that says

unknown is the type-safe counterpart of any

which really is the essence I think. :)

@drwpow
Copy link
Contributor

drwpow commented Jun 3, 2021

Thanks for providing more info. I also see that #625 was opened to address this. If it’s all the same to people, I don’t see why unknown isn’t an improvement. I’m on board with this change.

@drwpow
Copy link
Contributor

drwpow commented Sep 30, 2021

Added in #769!

@drwpow drwpow closed this as completed Sep 30, 2021
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

No branches or pull requests

2 participants