News for hackers. Stuff that matters.
Re:A 'CSS reset' for TypeScript, improving types for common ... by bluepnume on Friday March 03, 2023 @03:34AM (#35007166)
Those sound like the same thing to me.
Re:A 'CSS reset' for TypeScript, improving types for common ... by blowski on Friday March 03, 2023 @03:58AM (#35007338)
Consider a ‘dump’ function. By design, it handles anything you throw at it - strings, objects, booleans, etc. It won’t change. That’s ‘Any’.

Whereas ‘Unknown’ might be a stepping stone to introduce a more specific type once all the use cases are known. I let it handle strings and integers, log a line about any other type that calls it, and continue behaviour as it is today.

Re:A 'CSS reset' for TypeScript, improving types for common ... by dtech on Friday March 03, 2023 @04:46AM (#35007627)
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)
Re:A 'CSS reset' for TypeScript, improving types for common ... by badlucklottery on Friday March 03, 2023 @05:19AM (#35007817)
The 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
Re:A 'CSS reset' for TypeScript, improving types for common ... by capableweb on Friday March 03, 2023 @06:30AM (#35008217)
A 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".
Re:A 'CSS reset' for TypeScript, improving types for common ... by bigyikes on Friday March 03, 2023 @05:27AM (#35007862)
Here’s how I think about it, in much looser terms:

unknown = I don’t know

any = I don’t care

Often the implications are the same, but as you can intuit, any is a lot more relaxed.

Any will work anywhere because you just don’t care what the type is.

Unknown is stricter. It won’t work in cases where other types expect you to know what type you’re dealing with.

The compiler treats unknown very cautiously, whereas you can slap an any on pretty much anything.

Re:A 'CSS reset' for TypeScript, improving types for common ... by capableweb on Friday March 03, 2023 @06:29AM (#35008211)
They're the same as much as "1 + 1 = 2" and "2 + 0 = 2" is the same. The result might be the same ("any" and "unknown" accepting any type) but again, the intent is different (a value we know can be anything vs a value we don't know what type it'll have).