Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript says a React parameter implicitly has an 'any' type

ErrorParameter 'props' implicitly has an 'any' type.

Short answer

TypeScript raises this error when noImplicitAny is on and it cannot infer a parameter's type. In React it means a component's props, or a handler or helper written outside JSX, has no annotation. Give props an interface, type extracted handlers with React event types, annotate standalone helpers, and type useCallback parameters.

  • Applies to TypeScript 5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every occurrence at once without writing any files, then check whether strict mode is what turns the rule on.

    npx tsc --noEmit
    grep -n "strict\|noImplicitAny" tsconfig.json
  2. 2Match what you see, then jump to its fix

    1. 1The error points at the parameter list of a function component, for example function Card(props)The component takes props but never declares their typeFix
    2. 2The flagged parameter is named event or e, in a function declared above the return statement and passed as onChange or onClickAn event handler was moved out of the JSXFix
    3. 3The error is on the arrow function passed to useCallback, and tsconfig has strict set to trueA useCallback callback has an untyped parameter in strict modeFix
    4. 4The flagged parameter belongs to a helper such as const format = (value) => ..., not to a component or a callbackA standalone helper function that no typed API receivesFix

What does the error look like?

  • Parameter 'props' implicitly has an 'any' type.
  • Parameter 'event' implicitly has an 'any' type.

Cause 1 of 4

The component takes props but never declares their type

1Check

Open the flagged line. If the parameter has no colon and no type after it, TypeScript has nothing to infer from, because nothing passes the function a type.

npx tsc --noEmit | grep "implicitly has an 'any' type"

2Fix

Describe the props with an interface or type and put it on the parameter. An inline type also works for one or two fields.

interface CardProps {
  title: string;
  selected: boolean;
}

function Card({ title, selected }: CardProps) {
  return <div aria-selected={selected}>{title}</div>;
}

3Verify

Run the compiler again; the error for that component is gone.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

An event handler was moved out of the JSX

1Check

A handler written inline in the attribute gets its event type from the element. Once you extract it into its own function, that link is gone and the parameter is untyped.

grep -rn "function handle\|const handle" src --include=*.tsx

2Fix

Annotate the parameter with the matching React event type, with the element type as the argument.

function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
  setName(event.currentTarget.value);
}

<input value={name} onChange={handleChange} />

3Verify

The compiler accepts the handler and the attribute that receives it.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

A useCallback callback has an untyped parameter in strict mode

1Check

useCallback does not know which event the function will receive, so under strict mode its parameter needs a type.

grep -n "useCallback" -r src --include=*.tsx

2Fix

Pass the handler type as the generic argument of useCallback, or annotate the parameter directly.

const handleChange = useCallback<React.ChangeEventHandler<HTMLInputElement>>(
  (event) => {
    setName(event.currentTarget.value);
  },
  [setName]
);

3Verify

No error remains on that callback.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

A standalone helper function that no typed API receives

1Check

A callback passed to a typed function, such as the argument of map on a typed array, takes its parameter type from that function. A function declared on its own receives no such type, and a parameter without an annotation is implicitly any.

grep -rnE "= \(\w+\) =>" src --include=*.tsx

2Fix

Write the parameter type. Turning the rule off hides this class of bug, so reserve a tsconfig override for a short migration period and fix files one by one.

const formatPrice = (value: number): string => value.toFixed(2);

// temporary, while migrating a JavaScript project (not a fix):
// "noImplicitAny": false

3Verify

The compiler passes with noImplicitAny left on.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Keep strict on from the first commit of a TypeScript project. The default for noImplicitAny is true under strict, so new untyped parameters are caught while you write them.
  • Declare a Props interface next to every component before writing its body.
  • Prefer inline handlers for one-line changes. Extract a handler only when you are ready to type it.
  • Run tsc --noEmit in CI so an untyped parameter fails the build, not a code review.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.