Skip to content

[no-unnecessary-type-assertion] doesn't handle types properly -> React['key'] #982

Closed
@Hotell

Description

@Hotell

Repro

no-unnecessary-type-assertion is resolving key keys on React components inconsistently with normal function call. Please take a look at the code for better descrition.

{
  "rules": {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  }
}
const appTest = () => {
  type ArtificialKey = string | number;
  interface ReactAttributeArtificial {
    key?: ArtificialKey;
  }
  type Data = {
    id?: null | string | number;
  };

  const Test = (props: Data) => {
    // ✅no tsc errors
    // ✅no eslint errors
    return ChildFn({ key: props.id! });
  };

  const Test2 = (props: Data) => {
    // 🚨ESLINT error.
    // 🚨If you remove non null operator it will be an compile error
    return <ChildCmp key={props.id!} />;
  };

  const ChildFn = (props: ReactAttributeArtificial) => 'Hello';
  const ChildCmp = () => <div>Hello</div>;
};

Expected Result

It should behave consistently

Actual Result

JSX key is resolved in not correct way ?

image

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 2.2.0
@typescript-eslint/parser 2.2.0
TypeScript 3.6.3
ESLint 6.3.0
node 10
yarn 1.17

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