Mapped type compiler error when strict is true: Mapped object type implicitly has an 'any' template type

I have a mapped type:

export type Errors<T> = {
  [P in keyof T]?
}

strict is set to true in tsconfig.json and I get this error:

Mapped object type implicitly has an 'any' template type.

How can I get rid of this error without setting strict to false or disabling the error?

>Solution :

You are missing the type, so the default is for the mapped type to implicitly be any for all props. But under noImplictAny which is part of strict, TypeScript will not implicitly assume any

The simplest solution is to specify a type. To keep existing behavior you can use any but maybe consider a stricter type if possible.

export type Errors<T> = {
  [P in keyof T]?: any
}

Playground Link

Leave a Reply