My goal here is to create a function that can be used with or without arguments. If arguments are provided, it should work with those values; if not, default values should be used.
The issue I'm facing is that although there are no TypeScript errors in the code snippet below, when I call the function like testingInterfacesFunction(), I encounter the following error:
Cannot read properties of undefined (reading 'title')
interface Success {
onSuccess: (x: any) => void | null
successTitle: string
successSubtitle: string
successIcon: "success" | "info" | "error" | "question" | "warning"
}
interface Denial {
onDenied: (x: any) => void | null
denialTitle: string
denialSubtitle: string
denyIcon: "success" | "info" | "error" | "question" | "warning"
}
interface Dismiss {
onDismissed: (x: any) => void
dismissTitle: string
dismissSubtitle: string
dismissIcon: "success" | "info" | "error" | "question" | "warning"
}
interface Extra {
confirmButton: string
cancelButton: string
focus: boolean
}
interface Params {
title: string
subTitle: string
icon: "success" | "info" | "error" | "question" | "warning"
success: Success
deny: Denial
dismiss: Dismiss
extra: Extra
}
const testingInterfacesFunction = (params: Params) => {
const {
title = "",
subTitle = "",
icon = "question",
success: { onSuccess = null, successTitle = "", successSubtitle = "", successIcon = "success" },
deny: { onDenied = null, denialTitle = "", denialSubtitle = "", denyIcon = "error" },
dismiss: { onDismissed = null, dismissTitle = "", dismissSubtitle = "", dismissIcon = "error" },
extra: { confirmButton = "done", cancelButton = "cancel", focus = false }
} = params
... the body of the function ...
}
I even tried to use console.log for the title, but it seems like the function's body is not being executed at all...
All I want is to create a function that can optionally accept these properties and uses defaults if not provided. How can I achieve this?