Today i found out : optional typescript function props

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 …


This content originally appeared on DEV Community and was authored by Dennis kinuthia

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

interface Types{
person?:{name: string , age: number}
}


const 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

interface Types{
person?:{name: string , age: number}

sayHello?:(name: string)=>void
}
// Then execute the function like this

sayHello.?(person?.name)


This content originally appeared on DEV Community and was authored by Dennis kinuthia


Print Share Comment Cite Upload Translate Updates
APA

Dennis kinuthia | Sciencx (2022-07-14T01:32:58+00:00) Today i found out : optional typescript function props. Retrieved from https://www.scien.cx/2022/07/14/today-i-found-out-optional-typescript-function-props/

MLA
" » Today i found out : optional typescript function props." Dennis kinuthia | Sciencx - Thursday July 14, 2022, https://www.scien.cx/2022/07/14/today-i-found-out-optional-typescript-function-props/
HARVARD
Dennis kinuthia | Sciencx Thursday July 14, 2022 » Today i found out : optional typescript function props., viewed ,<https://www.scien.cx/2022/07/14/today-i-found-out-optional-typescript-function-props/>
VANCOUVER
Dennis kinuthia | Sciencx - » Today i found out : optional typescript function props. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/14/today-i-found-out-optional-typescript-function-props/
CHICAGO
" » Today i found out : optional typescript function props." Dennis kinuthia | Sciencx - Accessed . https://www.scien.cx/2022/07/14/today-i-found-out-optional-typescript-function-props/
IEEE
" » Today i found out : optional typescript function props." Dennis kinuthia | Sciencx [Online]. Available: https://www.scien.cx/2022/07/14/today-i-found-out-optional-typescript-function-props/. [Accessed: ]
rf:citation
» Today i found out : optional typescript function props | Dennis kinuthia | Sciencx | https://www.scien.cx/2022/07/14/today-i-found-out-optional-typescript-function-props/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.