Reputation: 333
Why in this situation "a" variable type - unknown, while absence of value is an undefined type?
function action<T>(value?: T): T | undefined {
return value;
}
let a = action();
Upvotes: 0
Views: 136
Reputation: 614
Use a default value for your generic type.
function action<T = undefined>(value?: T): T | undefined {
return value;
}
let a = action(); //undefined
let b = action(3); //number
Upvotes: 1