Matteo T.
Matteo T.

Reputation: 1408

Compiler errors that I can't suppress

I'm compiling my React/TypeScript app using tsc --build and, while it worked well during development with TypeScript errors ignored, I'm encountering numerous errors in build mode. I initially attempted to suppress these errors, but I've realized I need to fix them instead.

However, I'm facing some particularly cryptic error messages, like the one below:

error TS2322: Type 'string' is not assignable to type '"button" | "submit" | "reset"'.                           

           <button
               type="button"                                                                                                                                   
               ~~~~                                

  node_modules/@types/react/index.d.ts:3148:9                                                                                                                  
    3148         type?: "submit" | "reset" | "button" | undefined;                                                                                             
                 ~~~~                                                                                                                                          
    The expected type comes from property 'type' which is declared here on type 'DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>'

It's not a matter of "Type X is not assignable to type X"; rather, it's complaining that I'm providing a string type, which is larger than the required type. However, the actual type I'm providing is not a string—it precisely matches the required type!

I'm adding a bunch of // @ts-ignore as a workaround.

Could anyone help me understand the root cause of these errors? What might be causing TypeScript to produce such messages when the type actually matches?

Upvotes: 1

Views: 49

Answers (1)

venki
venki

Reputation: 413

Definitely looks like an issue with your TypeScript setup somehow, and not the code, and it's kinda hard to give advice from afar.

Some options to try:

  • A nicer workaround, is probably to coerce a smaller type to , either with type=("button" as const) or type=("button" as "button" | "submit" | "reset")
  • Try cleaning your build/dev setup to any extent possible, ie: remove build products, maybe upto and including re-installing all packages, or recloning the repo and reinstalling.

Upvotes: 2

Related Questions