For example in a function like this:
function someFunc(err: Error | undefined, data: string | undefined) {
if (err) throw err;
data; // Intellisense should show `string`, not `string | undefined`
}
I tried this:
function someFunc<
Err extends Error | undefined,
Data extends Err extends Error ? undefined : string
>(err: Err, data: Data) {
if (err) throw err;
data.length; // 'data' is possibly 'undefined'
}
It didn’t work.
So the question is if it’s even possible to do something like this, or should I just add another if
check for data
and move on?
New contributor
Saffren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.