Worth mentioning that you can achieve a good DX without new dependencies using JSDocs:
// @ts-check/** @type {import("react").VFC<{ name: string; age: number }>} */exportconstExample=({name,age})=>(<h3>{name} will be {age+3} years old in 3 years!
</h3>);
If you try to use Example, you'll get autocompletion of name and age in your editor, if you try to pass a string to age it will give you an error, and it will also warn you if you didn't pass one of those required properties. If you want to make them optional, you can add question marks:
// @ts-check/** @type {import("react").VFC<{ name?: string; age?: number }>} */exportconstExample=({name,age})=>(<h3>{name} will be {age+3} years old in 3 years!
</h3>);
Also, you can achieve the same using TS instead of JS with JSDocs, like this:
importtype{VFC}from"react";exportconstExample:VFC<{name?:string;age?:number}>=({name,age})=>(<h3>{name} will be {age+3} years old in 3 years!
</h3>);
The good thing about this approach is that:
You'll get errors in dev, instead of having to reach that component to discover that something is wrong with it in production.
You'll get a better experience in your editor.
You don't need a new runtime dependency (better for the user and for you).
Worth mentioning that you can achieve a good DX without new dependencies using JSDocs:
If you try to use
Example
, you'll get autocompletion ofname
andage
in your editor, if you try to pass a string toage
it will give you an error, and it will also warn you if you didn't pass one of those required properties. If you want to make them optional, you can add question marks:Also, you can achieve the same using TS instead of JS with JSDocs, like this:
The good thing about this approach is that:
Cheers!
Thanks for this in depth explanation! I learned something this! Great stuff.
you nailed it!