Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly set the type for property `d` in `path` element in a d3 react typescript application

I’m trying to draw a triangle using the d3 symbol() method and assigned it to a variable symbolTri.

import { symbol, symbolTriangle } from "d3"
const symbolTri = symbol().size(100).type(symbolTriangle)

export const Component = () => {
    // ...
    return (
        <g>
            <path d={symbolTri()} />
        </g>
    )
}

But while setting the d property a typescript error Type 'string | null' is not assignable to type 'string | undefined'. occurred. And while hovering over the error it shows 'd' value should be of type d?: string | undefined;. But I’m unable to set it properly. Any help, or suggestion would be really helpful.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The easiest solution, assuming that the output of symbolTri() cannot be null (which seems a reasonable assumption in this case), is to use the non-null assertion operator, which effectively tells the TypeScript compiler that this variable will never be null:

<path d={symbolTri()!} />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading