[React Typescript] Updating the Global Namespace for an Additional Attribute

Answer1215 / 2023-08-24 / 原文

I want to add a common attribute to dom element globally:

<>
  <div testId="123" />
  <audio testId="123" />
  <video testId="123" />
  <a testId="123" />
  <abbr testId="123" />
  <address testId="123" />
</>;

 

Solution:

declare global {
  namespace React {
    interface HTMLAttributes<T> {
      solutionTestId?: string;
    }
  }
}