3 ms·
Such a `dump` function would be 100% the same with unknown, since all types can be converted to top types (both any and unknown in TS)
by dtech 4y ago
Such a `dump` function would be 100% the same with unknown, since all types can be converted to top types (both any and unknown in TS)
- badlucklottery 4y agoThe difference comes when you interface with other well-typed code and want to convert from any/unknown down to a specific type. For example: function handleString(s: string) { //... } declare const a: any; declare const u: unknown; handleString(a); // works with zero intervention handleString(u); // error: Argument of type 'unknown' is not assignable to parameter of type 'string' handleString(u as string); // works again with explicit re-typing
- capableweb 4y agoA dump function accepting "unknown" is signalling "I'm not sure what this function could handle, probably a lot of different types" vs accepting "any" where it's telling you "I can accept any type you throw at me".