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?
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 .
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.
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.
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
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
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>
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>
)
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With