peggyjs / peggy

Peggy: Parser generator for JavaScript
https://peggyjs.org/
MIT License
906 stars 64 forks source link

Get rid of tsd #436

Closed markw65 closed 8 months ago

markw65 commented 11 months ago

Get rid of tsd by rewriting expectType in a way that lets vanilla typescript check the types with the same precision.

hildjj commented 8 months ago

This one is next after #430

hildjj commented 8 months ago

Rebase and changelog, please.

markw65 commented 8 months ago

Updated. I'll just note one other way to do this that might be a bit clearer. See typescript playground

Basically, instead of exactType()(x)(), we could use subtypeOf<T>()(x).is_exact. It's the same trick, but (in my opinion) it reads a bit better. Without the is_exact, it just checks for any subtype; with the is_exact it checks for exact types (the original exactType does the same if you omit the final () - but its less clear to readers what the () is there for)

hildjj commented 8 months ago

I don't have a strong opinion, so I'll defer to your taste. Feel free to send another PR if you decide you like another way better.

reverofevil commented 8 months ago

Well, either trick is using heavily unstable features of TypeScript.

I wouldn't recommend either approach for future rewrites, but for a small localized change to remove tsd this is already good.

markw65 commented 8 months ago

Well, either trick is using heavily unstable features of TypeScript

I checked it back to ts-3.3.3. Same behavior everywhere - so absent some statement of intent to change the behavior (perhaps you've seen something?) I think it's pretty stable. Its just currying; its hard to see how it could break without breaking huge amounts of typescript...

reverofevil commented 8 months ago

Sorry, I didn't mean the problem is with currying. Problem is with conditional types, and, more precisely, them lacking any foundation in type theory. They might look like they're working, but even that equality check is incorrect. There're "better" type equalities in TS, but no single correct one. Unfortunately even type equality in TS is still an open problem.

Mapped object types, conditional types, indexed types, type guards and, well, majority of TS's type system is unsound and inconsistent. It's best to avoid any of that if reliability is at stake.