Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript difference between T and "T extends unknown"

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?

like image 209
Suresh Avatar asked Dec 04 '25 23:12

Suresh


1 Answers

I believe these are more or less equivalent:

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type

unknown is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn’t assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.

like image 173
beautifulcoder Avatar answered Dec 07 '25 16:12

beautifulcoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!