Live data from Hacker News

`satisfies` is my favorite TypeScript keyword (2024)

sjer.red

211–217 of 217 posts

Re: `satisfies` is my favorite TypeScript keyword (2024)

#212

Earlier quoted context omitted.

Casting? Not really - i think you’d only need a couple type checks. Imo this is mostly useful for situations where you want to handle input validation (and errors) in the UI code and this function lives far away from ui code. Your point about clamping makes sense, and it’s probably worth doing that anyway, but without it being encoded in the type you have to communicate how the function is intended to be used some ot…

What about documentation as in docstring, function signature label and the like. I quite like CommonLisp, EmacsLisp, Go for that.

Yeah, that’s probably the best possible implementation outside of the type system. The issue with docstrings is that they aren’t checked. So you’ve communicated the api to the coder, and maybe even to the ide, but not to the compiler.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#213
post #24

Earlier quoted context omitted.

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.

> 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 As someone who came from a CS background, this kind of attitude is deeply mysterious. That seems like a type expression I'd expect a CS undergrad to be able to write - certainly if an SDE with 1-2 years experience was confused by it, I'd be advocating against their further promotion…

School makes you smart. Life makes you economical.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#214

Earlier quoted context omitted.

> 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 [infer First, ...(infer Rest)] ? First extends unknown[] ? [...First, ...FlatArr ] : [First, ...FlatArr ] : []; 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…

Here’s the fun part that I suspect many here are forgetting: if you want to write the function body , it will probably (or at the very least can ) look very similar! function flat() { // type FlatArr = let [first, ...rest] = this; return (this.length > 0) ? // Arg extends [infer First, ...(infer Rest)] ? Array.isArray(first) ? // First extends unknown[] ? [...first, ...flat(rest)] : // [...First, ...FlatArr ] : [firs…

Notably, the provided type for Array.prototype.flat doesn't actually provide the flat type for arrays of a known structure.

In other words, if you flatten `[string, [string, number]]` my example would give you `[string, string, number]` whereas the one in lib.es2019.array.d.ts would give you `(string | number)[]`

(my example, on the other hand, would only flatten a depth of 1, though a completely flattened type instead of just depth 1 could be represented by changing `[...First, ...FlatArr] :` to `[...FlatArr, ...FlatArr] :`

I haven't tried supporting a depth param, but I suspect it's possible.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#215

Earlier quoted context omitted.

Agree wholeheartedly. Writing TypeScript is better than JavaScript, but the lack of runtime protection is fairly problematic. However, there are libraries such as https://zod.dev , and you can adopt patterns for your interfaces and there's already a large community that does this.

Could you please elaborate on "patterns for your interfaces"?

Sure. You tend to think about the edges of your application.

1. Router

Tanstack Router: Supports runtime validation libraries such as z0d. So I have routes such as example.com/viewer/$uuid/$number, it should 400 if those aren't actually validate uuid and numbers.

React Router: Supports Types, but every type is a string because, well, they technically are, but this isn't useful in practice in my opinion. There are 3rd party libs such as: https://github.com/fenok/react-router-typesafe-routes

2. API

Lets say you're making your API public to clients you can't trust to send the correct data ( which probably also includes your own client ).

https://www.npmjs.com/package/express-openapi-validator

This library advertises validating both your input and your output

3. State

https://github.com/pmndrs/zustand/discussions/1722

4. Database

https://www.npmjs.com/package/prisma-zod-generator

5. Forms

https://medium.com/@toukir.ahamed.pigeon/react-hook-form-wit...

6. ENV

https://jfranciscosousa.com/blog/validating-environment-vari...

Obviously checks on the agent are primarily a DX/UX thing, whilst checks on the server step are also security controls.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#216

Earlier quoted context omitted.

The every day practice of software engineering has little to do with the academic discipline of computer science. What makes a good software engineer is not usually the same thing that makes a good CS major

Sure, but basic CS knowledge is an expectation in much of the software field (albeit less since the mid-2010's javascript boom). A lot of companies aren't going to hire you if you don't know the basics of data structures and algorithms

The skills required to get hired and the skills required to do the job are, again, not related
Post reply on HN