Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
What would be the correct type to use in this case?

I'm trying to create Header component and when I add prop "testId", I get following type. message -->

Type '{ children: ReactNode; testId: string | undefined; }' is not assignable to type 'IntrinsicAttributes & ClassAttributes & HTMLAttributes'. Property 'testId' does not exist on type 'IntrinsicAttributes & ClassAttributes & HTMLAttributes'

But when I use "id" instead of "testId", I don't get type error, which is weird. I would like to use "testId" for Heading prop and not sure what type I need to assign. What am I missing? https://codesandbox.io/s/flamboyant-leftpad-l3bhj?file=/src/App.tsx

import "./styles.css";
import * as React from "react";

interface HeadingProps extends React.HTMLAttributes<HTMLHeadingElement> {
  headingLevel: "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "p";
  children?: React.ReactNode;
  testId?: string;
}

const Heading: React.FunctionComponent<HeadingProps> = ({
  headingLevel: HeadingLevel = "h1",
  children,
  testId
}: HeadingProps) => {
  return <HeadingLevel testId={testId}>{children}</HeadingLevel>;
};

const App: React.FC<{ headingText: string; description: string }> = () => (
  <div>
    <Heading headingLevel="h2" testId="test-heading">
      Hello World!
    </Heading>
  </div>
);
export default App;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The problem is in the following line:

return <HeadingLevel testId={testId}>{children}</HeadingLevel>;

testId is not a valid HTML attribute. You shoud use testId as the Heading prop and use a JSON for custom attributes in the HeadingLevel component.

import "./styles.css";
import * as React from "react";

interface HeadingProps extends React.HTMLAttributes<HTMLHeadingElement> {
  headingLevel: "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "p";
  children?: React.ReactNode;
  testId?: string;
}

const Heading: React.FunctionComponent<HeadingProps> = ({
  headingLevel: HeadingLevel = "h1",
  children,
  testId
}: HeadingProps) => {
  const attrs = {"testId": testId}
  return <HeadingLevel {...attrs}>{children}</HeadingLevel>;
};

const App: React.FC<{ headingText: string; description: string }> = () => (
  <div>
    <Heading headingLevel="h2" testId="test-heading">
      Hello World!
    </Heading>
  </div>
);
export default App;
about 4 years ago · Juan Pablo Isaza Report

0

There is a typo in your access. You specify testId in the interface, but in the implementation you try to access testingId which does not match your interface signature. Also, using regular id worked because that's a valid attribute for all elements. Here's the corrected code:

import "./styles.css";
import * as React from "react";

interface HeadingProps extends React.HTMLAttributes<HTMLHeadingElement> {
  headingLevel: "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "p";
  children?: React.ReactNode;
  testId?: string;
}

const Heading: React.FunctionComponent<HeadingProps> = ({
  headingLevel: HeadingLevel = "h1",
  children,
  testId // typo was here
}: HeadingProps) => {
  return <HeadingLevel>{children}</HeadingLevel>;
};

const App: React.FC<{ headingText: string; description: string }> = () => (
  <div>
    <Heading headingLevel="h2" testId="test-heading">  {/* typo was here */}
      Hello World!
    </Heading>
  </div>
);

export default App;

TypeScript Playground Link

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!