Tslint: Unsafe use of expression of type 'any'.

Created on 5 Jan 2018  ·  3Comments  ·  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.8.0
  • __TypeScript version__: 2.6.2
  • __Running TSLint via__: Node.js API

TypeScript code being linted

const json: {} | null | undefined = JSON.parse(queryData);

Configuration

{
    "defaultSeverity": "error",
    "extends": [
        "tslint:all"
    ],
    "rules": {
    }
}

Actual behavior

Unsafe use of expression of type 'any'.

Expected behavior

I can do const json: any = JSON.parse(queryData); but i want to keep the no-any rules
How can i get the return of JSON.parse without get Unsafe use of expression of type 'any'.

Not A Bug

All 3 comments

I'm really not sure what you're expecting here... You want to use any, but want to keep the no-any rule? That completely contradicts each other. What prevents you from just declaring the matching type for your query data and then just do const json: YourType = JSON.parse(queryData);?

Thanks for reply. I was not clear
My goal is to keep the no-any rules and i search a way to get the result of JSON.parse(queryData) without warning.

Fo now i do json: any = JSON.parse(queryData); => but i have to disable the no-any.
If i do your solution : const json: YourType = JSON.parse(queryData);
I will have then have to disable the no-unsafe-any rules.

This i why in my first approach i try the : const json: {} | null | undefined = JSON.parse(queryData); Looking on the doc https://palantir.github.io/tslint/rules/no-unsafe-any/ => i will not have the no-unsafe-any and the no any warning.

But JSON.parse can also return other type like string so this solution is not the one.
I actually not certain that there is a solution but before prefere to ask you guys specialist.

@ohrrkan your use case is a good one - normally what folks do is JSON.parse(queryData) as MyType. Closing this as non-actionable for now, since there hasn't been much demand for any action on TSLint's part. Hope that helps!

Was this page helpful?
0 / 5 - 0 ratings