Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unsafe-any and unknown #4164

Closed
ThomasdenH opened this issue Sep 14, 2018 · 2 comments · Fixed by #4442
Closed

no-unsafe-any and unknown #4164

ThomasdenH opened this issue Sep 14, 2018 · 2 comments · Fixed by #4442

Comments

@ThomasdenH
Copy link
Contributor

Another unknown one:

Bug Report

  • TSLint version: 5.11.0
  • TypeScript version: 3.0.3
  • Running TSLint via: CLI

TypeScript code being linted

const json = '{ "property": "value" }';
const result: unknown = JSON.parse(json);

with tslint.json configuration:

{
  "extends": [
    "tslint:recommended"
  ],
  "jsRules": {},
  "rules": {
    "no-unsafe-any": true
  }
}

Actual behavior

The lint triggers: Unsafe use of expression of type 'any'.

Expected behavior

Since every type is assignable to unknown, the usage is not actually unsafe. The lint shouldn't trigger.

@JoshuaKGoldberg
Copy link
Contributor

We should note that although this visually looks similar to #3621, it's not the same. Assigning the result to unknown is a good idea.

@NN---
Copy link
Contributor

NN--- commented Dec 9, 2018

Another sample with exception:

function g(e: unknown): void {}

function f() {
	try {

	} catch (e) {
		g(e); // it is okay to call g
	}
}
``

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants