News for hackers. Stuff that matters.
Re:Why doesn't TypeScript properly type Object.keys? by mirekrusin on Saturday June 24, 2023 @07:31AM (#36457761)
Flow has exact object types as dedicated annotation/kind.

This problem is much bigger. You can for example easily imagine api where you provide where clause that is converted to sql form. You may declare type to expect only certain parameters, but it won't help you at the type level as anything extra can be passed. This can be abused to extract some sensitive information.

You're forced to emulate exactness by always destructuring objects - not great.

Re:Why doesn't TypeScript properly type Object.keys? by __ryan__ on Saturday June 24, 2023 @08:06AM (#36457968)

  You're forced to emulate exactness by always destructuring objects - not great.
It’s almost like using objects as enumerable maps is an anti-pattern.
Re:Why doesn't TypeScript properly type Object.keys? by toxik on Saturday June 24, 2023 @08:21AM (#36458070)
The problem has nothing to do with objects. The problem is, how do you type check something like sprintf without ad hoc type rules?
Re:Why doesn't TypeScript properly type Object.keys? by __ryan__ on Saturday June 24, 2023 @08:56AM (#36458314)
I don’t think I’m following well enough to provide a meaningful response.

This is not meant as an argument against what you’re saying, because I know you were just giving an example, but I found this and thought you may find it interesting: https://www.hacklewayne.com/a-truly-strongly-typed-printf-in...

Re:Why doesn't TypeScript properly type Object.keys? by jitl on Saturday June 24, 2023 @09:37AM (#36458583)
Typescript can check sprintf though using template string types: https://www.hacklewayne.com/a-truly-strongly-typed-printf-in...
Re:Why doesn't TypeScript properly type Object.keys? by nine_k on Saturday June 24, 2023 @10:19AM (#36458884)
Meanwhile, both Rust [1] and Haskell [2] manage to implement statically type-safe string interpolation.

[1]: https://willcrichton.net/notes/type-safe-printf/

[2]: https://hackage.haskell.org/package/formatting

Re:Why doesn't TypeScript properly type Object.keys? by Spivak on Saturday June 24, 2023 @11:42AM (#36459622)
Yes but the Rust example is an ad hoc type rule implemented behind the macro. You can make it type-safe but you lose the ability to have a formatting language in the string itself.
Re:Why doesn't TypeScript properly type Object.keys? by mirekrusin on Sunday June 25, 2023 @05:13AM (#36466287)
Rust has explicit support for it in the compiler, which is not great.

Zig does it the right way - it's defined in zig itself, no special cases in compiler like in Rust.

Re:Why doesn't TypeScript properly type Object.keys? by mirekrusin on Saturday June 24, 2023 @03:02PM (#36461697)
To extend on it a bit, flow has really good story with:

1. nominal types for classes (with correct variance/liskov substitution principles)

2. structural types for the rest

3. explicit annotation for exact structural types

This setup is brilliant.