Re:Why doesn't TypeScript properly type Object.keys?
by hellcow on Saturday June 24, 2023 @09:33AM
(#36458554)
> Usually the lie is in your own code or bad dependencies
The lie is almost always in an external API response from fetch (hence the complaint about “any” above).
> Could you provide examples?
Off the top of my head… Go’s stdlib json.Unmarshal and Rust’s Serde derive Deserialize.
Re:Why doesn't TypeScript properly type Object.keys?
by __ryan__ on Saturday June 24, 2023 @09:44AM
(#36458639)
The lie is when your code uses* the “any” value where a concrete type is expected.
I was misunderstanding your point with the deserialize.
Edit: “using” -> “uses”
Re:Why doesn't TypeScript properly type Object.keys?
by consilient on Saturday June 24, 2023 @06:34PM
(#36463243)
> Usually the lie is in your own code or bad dependencies when an “unknown” type (including “any”) is cast to a concrete type without being validated.
Yes, but one of those bad dependencies is the standard library.
Re:Why doesn't TypeScript properly type Object.keys?
by __ryan__ on Saturday June 24, 2023 @08:22PM
(#36463999)
When does the standard library lie in this case?
Re:Why doesn't TypeScript properly type Object.keys?
by consilient on Saturday June 24, 2023 @10:40PM
(#36464706)
Things are `any` when they should be `unknown` or generic, mostly. Off the top of my head:
- `JSON.parse` and `.json()` on response bodies both return `any`.
- `JSON.stringify`'s `replacer` argument is of type `(key: any, string: any) => any`.
- `PromiseRejectedResult["reason"]` is `any`.
There are certainly many others.
The correct type for values you don’t know the type of (like the response of an API call) is “unknown”.
TypeScript does not provide the facilities you describe because there is not a one-size-fits-all solution to the cases that are possible and common in JavaScript.
It is left to the developer to decide how to validate unknown data at the boundaries of the API.
There are third party libraries that facilitate this in different ways with different trade-offs.
I find this to be rare if you are using strict mode with proper TypeScript definition files for your platform and dependencies. Usually the lie is in your own code or bad dependencies when an “unknown” type (including “any”) is cast to a concrete type without being validated. Could you provide examples? I either don’t understand or I disagree.