Skip to content

[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

Closed
ghost opened this issue Jul 25, 2020 · 6 comments · Fixed by #2371
Closed

[no-unsafe-assignment/return] Relax restrictions to allow any => unknown assignment #2325

ghost opened this issue Jul 25, 2020 · 6 comments · Fixed by #2371
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@ghost
Copy link

ghost commented Jul 25, 2020

Repro

function myFunction(): unknown {
    // triggers no-unsafe-assignment because JSON.parse returns any
    let val: unknown = JSON.parse(someStringVar);

    // triggers no-unsafe-return because JSON.parse returns any
    return JSON.parse(someStringVar);
}

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 use any, including the TypeScript library itself, and we have to make do with what we have.

@ghost ghost added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 25, 2020
@bradzacher bradzacher added enhancement New feature or request good first issue Good for newcomers and removed triage Waiting for team members to take a look labels Jul 25, 2020
@bradzacher
Copy link
Member

Happy to accept a PR!

@yasarsid
Copy link
Contributor

yasarsid commented Aug 7, 2020

Hi @bradzacher - If its all right, I want to work on this.

@maletor
Copy link

maletor commented Aug 15, 2020

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?

@maletor
Copy link

maletor commented Aug 15, 2020

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.

@bradzacher
Copy link
Member

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.

@maletor
Copy link

maletor commented Aug 15, 2020

I'll give 'runtypes' a try but it seems like #21732 may be close after a hard long two years.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants