How to make passed in parameter optionally undefined in TypeScript

Advertisements

I have a declaration like the following that works, but I feel like there is a simpler way to do it.

function getData(
  slug: string | undefined = undefined
) {

I would have thought this would work but it gives me syntax errors

function getData(
  slug: string? = undefined
) {

>Solution :

What about:

function getData(
  slug?: string
) {

In this way you can call the function with getData('something') or getData()

Leave a ReplyCancel reply