In typescipt, what is the difference between between T and T extends unknown when used as type parameter. For example:
function check<T extends unknown>(x: T): T {
return x;
}
vs
function check<T>(x: T): T {
return x;
}
Is there any difference between them in terms of behaviour?
I believe these are more or less equivalent:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type
unknownis the type-safe counterpart of any. Anything is assignable tounknown, butunknownisn’t assignable to anything but itself andanywithout a type assertion or a control flow based narrowing. Likewise, no operations are permitted on anunknownwithout first asserting or narrowing to a more specific type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With