Skip to content

[no-unnecessary-type-assertion] False positive with null/undefined union type #1061

Closed
@codebutler

Description

@codebutler

Looks like the fix for #529 did not cover all edge cases.

{
  "rules": {
      "@typescript-eslint/no-unnecessary-type-assertion": "error",
  }
}
const optionalNonNull = (s?: string) => { };
let s: string | null | undefined;

/*
Argument of type 'string | null | undefined' is not assignable to parameter of type 'string | undefined'.
  Type 'null' is not assignable to type 'string | undefined'.
*/
optionalNonNull(s);

/*
This assertion is unnecessary since the receiver accepts the original type of the expression.
*/
optionalNonNull(s!);

Expected Result

Should not trigger an error.

Actual Result

Triggers an error.

Additional Info

N/A

Versions

"@typescript-eslint/eslint-plugin": "^2.3.3",

Also tested with master 054df27

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions