-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[no-unsafe-assignment/return] Relax restrictions to allow any => unknown assignment #2325
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
Comments
Happy to accept a PR! |
Hi @bradzacher - If its all right, I want to work on this. |
This is awesome! But I got down a rabbit hole where you can't take 'unknown' back to a structured object like an interface. You can take 'unknown' to 'object' but you're stuck there. Even doing 'prop in object' doesn't work. Whats the reason? Is there a workaround? |
Addendum: I found user defined type guards but those seem really error prone. What happens if the interface changes and the type guard doesn't? Seems like it's of little use for writing safe code and a olive branch for lack of real type system. |
In this regard, unknown and any are the same. The difference being that unknown ensures you don't do things that are unsafe. Not being able to convert past object is a known issue that they haven't solved just yet. microsoft/TypeScript#21732 Essentially though, yes, you've run into the issue with typing an untyped boundary. There is no simple way to do this unfortunately. There are some libraries to help generate validation functions from type definitions, but there's no perfect solution. |
I'll give 'runtypes' a try but it seems like #21732 may be close after a hard long two years. |
Repro
Expected Result
This should not throw an error because a direct any => unknown cast cannot possibly be unsafe. Requiring an
as unknown
everywhere is unnecessary noise.Additional notes
Ideally,
any
should not be used across a code base, and I like that the current default rules enforce this. However, a lot of existing APIs still useany
, including the TypeScript library itself, and we have to make do with what we have.The text was updated successfully, but these errors were encountered: