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

no-useless-cast with Map.get #839

Open
AntonShchemelev opened this issue Mar 13, 2019 · 0 comments
Open

no-useless-cast with Map.get #839

AntonShchemelev opened this issue Mar 13, 2019 · 0 comments

Comments

@AntonShchemelev
Copy link

I want to report a bug.

Node.js version: 11.11.0

TypeScript version: 3.3.3333

tslint-sonarts version and used IDE: 1.9.0 Visual Studio Code latest

TSLint version: 5.14.0

Rule key: no-useless-cast

Reproducer

const m = new Map<string, string>();
m.set("x", "y");
const y = m.get("y")!.length; // False-positive here
console.log(y);

Logs

Remove this unnecessary not-null assertion. (no-useless-cast)tslint(1)

Expected behavior

tslint-sonarts should not give the warning because if I remove not null assertion operator ! the warning is gone but TypeScript fails to compile the code with the error:

error TS2532: Object is possibly 'undefined'.
const y = m.get("y").length;
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants