Back to blogs
Blog
Writing in public
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Blog
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Back to blogs
Back to blogs
Posttypescript, react, javascript, programming
While writing a custom table component for react i ran into an interesting issue . The table takes...
Date published

Discriminated unions (also known as tagged unions) are a powerful TypeScript pattern that enables...

slides used in the presentation building the generic component In this example we'll use...

Why You Might Still Pick Next.js Over TanStack Start From a TanStack Start...
While writing a custom table component for react i ran into an interesting issue .
The table takes in props like and array of objects that define the header and column of the table and an array of objects for the rows , with those props the component is able to render a table .
But the point of writing a custom table is to add features not available in a regular html table , so i needed to pass in functions to handle the editing which are optional since they'd only need to be used when the table is in edit mode
Normally in typescript when you have a possibly undefined variable yon can use
1interface Types{2person?:{name: string , age: number}3}456const age = person?.age
This helps you avoid the cannot access .age of undefined error that normally breaks your code. This implementation won't try to access the variable if it's undefined
Something like that exists for functions enabling you to have possibly undefined functions without the function cannot be undefined error
1interface Types{2person?:{name: string , age: number}34sayHello?:(name: string)=>void5}6// Then execute the function like this78sayHello.?(person?.name)
check out the table at repo