4 ms·
What’s an example complex generic? If you have one, that is, I’m curious what kinds of things people do with types. I’m a fan of using types, myself. Thanks in
by ok_dad 2y ago
What’s an example complex generic? If you have one, that is, I’m curious what kinds of things people do with types. I’m a fan of using types, myself. Thanks in advance.
- bobbylarrybobby 2y agoCheck out the implementations of axum’s Handler trait in Rust. https://docs.rs/axum/0.7.5/axum/handler/trait.Handler.html https://docs.rs/axum/0.7.5/axum/handler/trait.Handler.html
- foresterre 2y agoI sometimes see types in TS with complex generics like [1], but instead of being general like in this example, they're more specific to the type of their values. Couldn't find a proper example on the spot in an open source project. [1] https://github.com/sindresorhus/type-fest/blob/main/source/get.d.ts#L25 https://github.com/sindresorhus/type-fest/blob/main/source/g...
- chamomeal 2y agoHonestly any typescript library with great type safety probably has seriously gnarly generics inside. Here[1] is a random file from kysely, which is surprisingly readable even though kysely's type safety is incredibly good. But things I encounter in the regular non-library world are usually recursive types that have specific constraints. A couple years ago I had my first foray into typescript generics, and was so stumped that I actually gave up. I was trying to map the type of one nested object to another. This[2] is the stackoverflow post from the legendary jcalz that saved me! Check jcalz's link to the TS playground [1]https://github.com/kysely-org/kysely/blob/master/src/query-builder/output-interface.ts https://github.com/kysely-org/kysely/blob/master/src/query-b... [2]https://stackoverflow.com/questions/72461962/is-it-possible-to-recursively-map-one-json-like-type-to-another-in-typescript https://stackoverflow.com/questions/72461962/is-it-possible-...
- Frotag 2y agoCommon usecase is mapping database schemas to "this is what SELECTs / INSERTs" should contain. Honestly most complicated types boil down to a handful of concepts (mapped types, conditionals, recursion) with a few tricks for working around sharp corners (deferring evaluation for performance, controlling whether a union distributes). I've even seen someone solve 8-queens in types using a formal grammar but no clue how that worked. Speaking of, if anyone else has other examples of defining a grammar or ast in typescript, I'd love to see it.
- Frotag 2y agoCommon usecase is mapping database schemas to "this is what SELECTs / INSERTs" should contain. eg https://github.com/jakearchibald/idb https://github.com/jakearchibald/idb Honestly most complicated types boil down to a handful of concepts (mapped types, conditionals, recursion) with a few tricks for working around sharp corners (deferring evaluation for performance, controlling whether a union distributes). I've even seen someone solve 8-queens in types using a formal grammar but no clue how that worked. Speaking of, if anyone else has other examples of defining a grammar or ast in typescript, I'd love to see it.
- steveklabnik 2y agoI don't want to scare you... but here's a function from a codebase at work. https://github.com/oxidecomputer/omicron/blob/5fd1c3588a45adfba0d8c59ff847487b28e6968b/nexus/db-queries/src/db/pagination.rs#L78-L181 https://github.com/oxidecomputer/omicron/blob/5fd1c3588a45ad... That's ninety lines of type constraints.
- chamomeal 2y agoOh god…