| ▲ | pcthrowaway 12 hours ago |
| > Hell, ask someone to write a signature for array flat, you'd be surprised how many would fail. To be clear, an array flat type: type FlatArr<Arg extends unknown[]> = Arg extends [infer First, ...(infer Rest)] ?
First extends unknown[] ?
[...First, ...FlatArr<Rest>] :
[First, ...FlatArr<Rest>] :
[];
is far from basic Typescript. The average Typescript dev likely doesn't need to understand recursive conditional types. It's a level of typescript one typically only needs for library development.Not only have I never been expected to write something like this for actual work, I'm not sure it's been useful when I have, since most of my colleagues consider something like this nerd sniping and avoid touching/using such utilities, even with documentation. |
|
| ▲ | wk_end 12 hours ago | parent | next [-] |
| If I saw that in a PR I would push very hard to reject; something like that is a maintenance burden that probably isn’t worth the cost, and I’ve been the most hardcore about types and TypeScript of anyone of any team I’ve been on in the past decade or so. Now, that said, I probably would want to be friends with that dev. Unless they had an AI generate it, in which case the sin is doubled. |
| |
| ▲ | probabletrain 11 hours ago | parent | next [-] | | I think there’s a difference between what’s expected/acceptable for library code vs application code. Types like this might be hard to understand, but they create very pleasant APIs for library consumers. I’ve generally found it very rare that I’ve felt the need to reach for more complex types like this in application code, however. RXJS’s pipe function has a pretty complex type for its signature, but as a user of the library it ‘just works’ in exactly the type-safe way I’d expect, without me having to understand the complexity of the type. | |
| ▲ | pjerem 2 hours ago | parent | prev | next [-] | | I’d say it depends. I always advocate for code that is easy to read and to understand, but in extremely rare conditions, hard to read code is the better solution. Especially when it comes to signatures in Typescript, complex signatures can be used to create simple and ergonomic APIs. But anyway you shouldn’t be allowed to push anything like this without multiple lines of comments documenting the thing. Unreadable code can be balanced with good documentation but I rarely saw this unfortunately. | |
| ▲ | ibejoeb 11 hours ago | parent | prev | next [-] | | If it's correct, it's not a maintenance nightmare, and it will alert you to problems later when someone wants to use it incorrectly. If you're writing first-party software, it probably doesn't matter. But if you have consumers, it's important. The compiler will tell you what's wrong all downstream from there unless someone explicitly works around it. That's the one you want to reject. | | |
| ▲ | locknitpicker 3 hours ago | parent [-] | | > If it's correct, it's not a maintenance nightmare, and it will alert you to problems later when someone wants to use it incorrectly. You're confusing things. It is a maintenance nightmare because it is your job to ensure it is correct and remains correct in spite of changes. You are the one owning that mess and held accountable for it. > If you're writing first-party software, it probably doesn't matter. But if you have consumers, it's important. Yes, it is important that you write correct and usable code. That code doesn't fall on your lap though and you need to be the one writing and maintaining it. Whoever feels compelled to write unintelligible character soup that makes even experienced seasoned devs pause and focus is failing their job as a software engineer. |
| |
| ▲ | spankalee 11 hours ago | parent | prev | next [-] | | What's the alternative? Have incorrect types for the function? That's not better. | | |
| ▲ | epolanski 10 hours ago | parent | next [-] | | The alternative is what shows in the comment: go on HN and tell the world you think TS and JS are crap and it's not worth your time, while writing poor software. | |
| ▲ | wk_end 10 hours ago | parent | prev [-] | | To answer this we probably need more details, otherwise it's gonna be an XY Problem. What is it that I'm trying to do? How would I type this function in, say, SML, which isn't going to allow incorrect types but also doesn't allow these kinds of type gymnastics? | | |
| ▲ | spankalee 9 hours ago | parent [-] | | We don't have to deal in hypotheticals - we have a concrete example here. There's a method, array.flat() that does a thing that we can correctly describe in TypeScript's type system. You say you would reject those correct types, but for what alternative? It's hugely beneficial to library users to automatically get correctly type return values from functions without having to do error-prone casts. I would always take on the burden of correct types on the library side to improve the dev experience and reduce the risk of bugs on the library-consumption side. | | |
| ▲ | wk_end 9 hours ago | parent [-] | | There's nothing I can do about the standard JavaScript library, but in terms of code I have influence over, I very simply would not write a difficult-to-type method like Array.prototype.flat(), if I could help it. That's what I mean by an XY Problem - why are we writing this difficult-to-type method in the first place and what can we do instead? Let's suppose Array.prototype.flat() wasn't in the standard library, which is why I'm reviewing a PR with this gnarly type in it. If I went and asked you why you needed this, I guess you'd say the answer is: "because JavaScript lets me make heterogenous arrays, which lets me freely intermix elements and arrays and arrays of arrays and... in my arrays, and I'm doing that for something tree-like but also need to get an array of each element in the structure". To which I'd say something like "stop doing that, this isn't Lisp, define an actual data type for these things". Suddenly this typing problem goes away, because the type of your "flatten" method is just "MyStructure -> [MyElements]". | | |
| ▲ | pcthrowaway 8 hours ago | parent | next [-] | | Sure, if you're living fully in your own application code, and you don't need to consume things from an API you don't control, it's easy to live in a walled garden of type purity. I can recognize that most people are going to go for inaccurate types when fancier semantics are necessary to consume things from the network. But we also have the real world where libraries are used by both JS devs and TS devs, and if we want to offer semantics that idiomatic for JS users (such as Array.prototype.flat()) while also providing a first-class experience to TS consumers, it is often valuable to have this higher-level aptitude with the TS type system. As mentioned earlier, I believe 90% of TS devs are never in this position, or it's infrequent enough that they're not motivated to learn higher-level type mechanics. But I also disagree with the suggestion that such types should be avoided because you can always refactor your interface to provide structure that allows you to avoid them; You don't always control the shape of objects which permeate software boundaries, and when providing library-level code, the developer experience of the consumer is often prioritized, which often means providing a more flexible API that can only be properly typed with more complex types. | |
| ▲ | kaoD 2 hours ago | parent | prev | next [-] | | > Suddenly this typing problem goes away, because the type of your "flatten" method is just "MyStructure -> [MyElements]". How is that less maintenance burden than a simple Flatten type? Now you have to construct and likely unwrap the types as needed. And how will you ensure that you're flattening your unneeded type anyways? Sure you can remove the generics for a concrete type but that won't simplify the type. It's simple. It's just recursive flattening an array in 4 lines. Unlikely to ever change, unlike the 638255 types that you'd have to introduce and maintain for no reason. There are many reasons not to do that. Say your business logic changes and your type no longer needs one of the alternatives: you are unlikely to notice because it will typecheck even if never constructed and you will have to deal with that unused code path until you realize it's unused (if you ever do). You made code harder to maintain and more complex for some misguided sense of simplicity. | |
| ▲ | 2 hours ago | parent | prev | next [-] | | [deleted] | |
| ▲ | ffsm8 6 hours ago | parent | prev | next [-] | | > MyStructure -> [MyElements] Right, from the structure you get an array with one element which is likely an union type from that naming. Honestly, you sound more like your arguing from the perspective of a person unwilling to learn new things, considering you couldn't even get that type correct. To begin with, that flat signature wasn't even hard to understand? | | |
| ▲ | wk_end 6 hours ago | parent | next [-] | | What I wrote would be a syntax error in TypeScript (no name for the argument, wrong arrow), not a function that returns array with one element; I used Haskell-ish notation instead of TypeScript's more verbose "(structure: MyStructure) => MyElement[]". I thought it was clear enough that I was being informal and what I meant was clear, but that was admittedly probably a mistake. But to infer an implication from that that I'm "unwilling to learn new things" is a non sequitur and honestly kind of an unnecessarily dickish accusation. | |
| ▲ | lovich 6 hours ago | parent | prev [-] | | Brah, If you have a type with that many characters in it that isn’t a super long string name, it’s not easy to understand unless you are the 1% of 1% when it comes to interpreting this specific language. On top of that I fully agree with the poster you’re responding to. In general application code that’s and extremely complicated type, generally done by someone being as clever as can be. And if the code you’ve written when you’re being as clever as possible has a bug in it, you won’t be clever enough to debug it. |
| |
| ▲ | geoffmanning 8 hours ago | parent | prev [-] | | This. 1000%. |
|
|
|
| |
| ▲ | 8note 12 hours ago | parent | prev | next [-] | | looking back at them is also real hard to debug. you dont get a particularly nice error message, and a comment or a test would tell better than the type what the thing should be looking like | |
| ▲ | 12 hours ago | parent | prev [-] | | [deleted] |
|
|
| ▲ | epolanski 10 hours ago | parent | prev | next [-] |
| The version I was thinking when I wrote the comment is simpler type Flatten<T> = T extends Array<infer U> ? Flatten<U> : T
> The average Typescript dev likely doesn't need to understand recursive conditional types.The average X dev in Y language doesn't need to understand Z is a poor argument in the context of writing better software. |
| |
| ▲ | furyofantares 9 hours ago | parent | next [-] | | > The average X dev in Y language doesn't need to understand Z is a poor argument in the context of writing better software. It's a good response to the claim that we'd be surprised at how many would fail to do this, though. | |
| ▲ | NooneAtAll3 9 hours ago | parent | prev [-] | | as a person that never touched JS and TS... what's the difference between the two answers? | | |
| ▲ | granzymes 9 hours ago | parent | next [-] | | For one, the simple answer is incomplete. It gives the fully unwrapped type of the array but you still need something like type FlatArray<T extends unknown[]> = Flatten<T[number]>[]
The main difference is that the first, rest logic in the complex version lets you maintain information TypeScript has about the length/positional types of the array. After flattening a 3-tuple of a number, boolean, and string array TypeScript can remember that the first index is a number, the second index is a boolean, and the remaining indices are strings. The second version of the type will give each index the type number | boolean | string. | |
| ▲ | ameliaquining 9 hours ago | parent | prev | next [-] | | First one flattens a potentially-nested tuple type. E.g., FlatArr<[number, [boolean, string]]> is [number, boolean, string]. Second one gets the element type of a potentially-nested array type. E.g., Flatten<number[][]> is number. For what it's worth, I've never needed to use either of these, though I've occasionally had other uses for slightly fancy TypeScript type magic. | |
| ▲ | pcthrowaway 4 hours ago | parent | prev [-] | | The answer above actually gets the type union of all non-array elements of a multi-level array. In other words Flatten<[1,[2,'a',['b']]]>
will give you a union type of 1, 2, 'a', and 'b' const foo: Flatten<[1,[2,'a',['b']]]> = 'b'; // OK
const bar: Flatten<[1,[2,'a',['b']]]> = 'c'; // Error: Type '"c"' is not assignable to type '1 | 2 | "a" | "b"'
Technically the inference is unnecessary there, if that's you're goal: type Flatten<T> = T extends Array<unknown> ? Flatten<T[number]> : T
I don't really consider this the type of flattening an array, but `Array<Flatten<ArrType>>` would be. And this would actually be comparable to the builtin Array.prototype.flat type signature with infinite depth (you can see the typedef for that here[1], but this is the highest level of typescript sorcery)My solution was for flattening an array with a depth of 1 (most people using Array.prototype.flat are using this default depth I'd wager): console.log(JSON.stringify([1,[2, [3]]].flat()));
> [1,2,[3]]
The type I provided would match those semantics: // 'readonly' added to the 'extends' sections to work on "as const" (readonly) arrays
type FlatArr<Arg extends unknown[] | readonly unknown[]> = Arg extends readonly [infer First, ...(infer Rest)] ?
First extends readonly unknown[] ?
[...First, ...FlatArr<Rest>] :
[First, ...FlatArr<Rest>] :
[];
const flatten = <Arr extends unknown[] | readonly unknown[]>(arr: Arr): FlatArr<Arr> => arr.flat() as FlatArr<Arr>
const someArr = [1,[2,'a',['b', ['c']]]] as const; // const someArr: readonly [1, readonly [2, "a", readonly ["b", readonly ["c"]]]]
const someArr2 = flatten(someArr); // const someArr2: [1, 2, "a", readonly ["b", readonly ["c"]]]
[1]: https://github.com/microsoft/TypeScript/blob/main/src/lib/es... |
|
|
|
| ▲ | miki123211 11 hours ago | parent | prev | next [-] |
| I recently had to write a Promise.all, but using an object instead of an array. That was... non-trivial. |
| |
| ▲ | hdjrudni 10 hours ago | parent | next [-] | | If it's what I'm thinking, that one isn't too bad. I wrote it awhile back: export async function promiseAll<T extends Record<string, Promise<any>>>(promises: T): Promise<{ [K in keyof T]: Awaited<T[K]> }> {
const keys = Object.keys(promises) as Array<keyof T>;
const result = await Promise.all(keys.map(key => promises[key]));
return Object.fromEntries(result.map((value, i) => [keys[i], value])) as { [K in keyof T]: Awaited<T[K]> };
| | |
| ▲ | atsjie 3 hours ago | parent [-] | | I'd call that bad pretty bad. Without internet or AI I wouldn't attempt writing anything like that. |
| |
| ▲ | ruined 9 hours ago | parent | prev [-] | | rejoice https://github.com/tc39/proposal-await-dictionary |
|
|
| ▲ | kaoD 12 hours ago | parent | prev [-] |
| For those unfamiliar with TS, the above is just... function flat([head, ...tail]) {
return Array.isArray(head)
? [...flat(head), ...flat(tail)]
: [head, ...flat(tail)]
}
...in TS syntax. |
| |
| ▲ | tomsmeding 11 hours ago | parent [-] | | Well, it is the type of that, in TS syntax. Few are the statically-typed languages that can even express that type. | | |
| ▲ | not_kurt_godel 7 hours ago | parent [-] | | Java: List<Object> Python: list[Any] ...what am I missing? | | |
| ▲ | rjh29 3 hours ago | parent | next [-] | | You're missing the specialisation of Object/Any. For example Array.flat called with [int, [bool, string]] returns a type [int, bool, string]. Admittedly this is somewhat niche, but most other languages can't express this - the type information gets erased. | |
| ▲ | sli 4 hours ago | parent | prev [-] | | You're missing the input type, essentially. Those are just array types. The TypeScript type signature more of a function type, it expresses flattening a n-dimensional array (input type) into a flat array (output type). |
|
|
|