Vladislav Khazipov
Vladislav Khazipov

Reputation: 333

Unexpected unknown type in the empty function call

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

Answers (1)

tszarzynski
tszarzynski

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

Related Questions