Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare/ignore a missing JSX type?

Tags:

typescript

jsx

I would like to use the SVG use element in a .tsx file:

<mask id="mask1">
    <use ... />
</mask>

I'm transpiling this to React calls directly. However, I keep receiving the following error:

Property 'use' does not exist on type 'JSX.IntrinsicElements'.

I believe this element is missing from the standard lib definitions. How can I declare this type for the TypeScript compiler to see, similar to how I can declare a class or variable? The usual declare var and declare class statements have no effect.

If that's not possible, how can I make the TypeScript compiler ignore this error?

like image 522
John Weisz Avatar asked Oct 25 '16 16:10

John Weisz


People also ask

How do I make TypeScript ignore an error?

Use // @ts-ignore to ignore the type checking errors on the next line in a TypeScript file. If you use a linter, you might have to add a comment to also suppress linting errors when using ts-ignore - // eslint-disable-next-line @typescript-eslint/ban-ts-comment .

How do you use TS Nocheck?

Use the // @ts-nocheck comment to disable type checking for an entire file in TypeScript. The // @ts-nocheck comment instructs TypeScript to skip the file when type checking. If you use a linter, you might need to disable it on the line of the comment.

What is JSX element type?

JSX is an embeddable XML-like syntax. It is meant to be transformed into valid JavaScript, though the semantics of that transformation are implementation-specific. JSX rose to popularity with the React framework, but has since seen other implementations as well.


5 Answers

If you want to ignore an error inline, use this weird syntax:

{/* 
// @ts-ignore */}
<Foo id="fooDoesNotHaveIdType" /> 

Taken from https://github.com/Microsoft/TypeScript/issues/27552

like image 165
Mikey Avatar answered Oct 13 '22 23:10

Mikey


I tried @Mikey's solution it did not work, but the following works for me:

    <Foo
      /*
      // @ts-ignore */
      id="foo does not have IdType"
      baz="foo Have bazType"
    /> 

In the above I am ignoring type error in property id

like image 37
apollo Avatar answered Oct 13 '22 22:10

apollo


A use inside mask no longer throws a type error but if it still did today you could:

<mask id="mask1">
    {/* @ts-ignore */} 
    <use ... />
</mask>
like image 22
vhs Avatar answered Oct 13 '22 21:10

vhs


You can use @ts-ignore:

{/* @ts-ignore */} 
<mask id="mask1">
  <use ... />
</mask>

It also works if you use inline conditions:

{
  /* @ts-ignore */ myCondition && (
    <mask id="mask1">
      <use ... />
    </mask>
  )
}
like image 39
Giovanni Benussi Avatar answered Oct 13 '22 23:10

Giovanni Benussi


I'm transpiling this to React calls directly

One workaround is using React.createElement directly:

<mask id="mask1">
    {React.createElement("use", { ... })}
</mask>

This isn't pretty, but it gets the job done, and the compiled output is essentially the same.

like image 26
John Weisz Avatar answered Oct 13 '22 21:10

John Weisz