Skip to content

Instantly share code, notes, and snippets.

@minajevs
Last active October 15, 2018 14:21
Show Gist options
  • Save minajevs/e93f1ad12160ebfadd77185d68dac6dc to your computer and use it in GitHub Desktop.
Save minajevs/e93f1ad12160ebfadd77185d68dac6dc to your computer and use it in GitHub Desktop.
function foo<T>(arg: T) {
if (arg === undefined)
doThis()
else
doThat(arg)
}
// number
foo<number>(42) // OK
foo<number>() // ERROR: 1 argument expected
foo<number>("bar") // ERROR: 'bar' is not assignable 'number'.
// undefined
foo<undefined>() // ERROR: 1 argument expected, BUT we would like it to be possible!
foo<undefined>(41) // ERROR: '42' is not assignable to 'undefined'
foo<undefined>(undefined) // OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment