fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.35k stars 204 forks source link

Preserve `@ts-expect-error` directives #776

Closed alecmev closed 3 months ago

alecmev commented 3 months ago

As mentioned in https://github.com/fabian-hiller/valibot/issues/659#issuecomment-2183564529, some prefer to keep skipLibCheck off. It's not about being pedantic, it can help explain otherwise cryptic errors (speaking from experience), and puts pressure on the ecosystem to keep their types clean.

However, in this case it isn't an easy fix at all (looks like QuestionMarkSchema being recursive pushes the limits of inference, https://github.com/microsoft/TypeScript/issues/30134, https://github.com/microsoft/TypeScript/issues/47599). Hit my head for half an hour, to no avail.

Thankfully, this repo already has // @ts-expect-error peppered throughout:

https://github.com/fabian-hiller/valibot/blob/094d9845e4d5c90659ac61d2627dba811568d07a/library/src/types/other.ts#L90-L99

So all that needs to be done is just preserve these in the emitted .d.ts. Easier said than done, as I haven't been able to find if tsc can even do it, but worst case, at least it could be a post-build script just adding these back in above QuestionMarkSchema and QuestionMarkSchemaAsync, and inside of TuplePath.

fabian-hiller commented 3 months ago

A better solution would be to figure out how to avoid the recursive inference problem. If we can't find a solution, we could try to limit the recursiveness to a certain depth. Are you interested in exploring this problem?

alecmev commented 3 months ago

Done 😉 https://github.com/fabian-hiller/valibot/pull/778

fabian-hiller commented 3 months ago

Related issue at the TS repo: https://github.com/microsoft/TypeScript/issues/38628

alecmev commented 3 months ago

Good find! Looking at that issue, how about /** @ts-expect-error */? TypeScript preserves doc comments.

fabian-hiller commented 3 months ago

This may work, but it is a dirty hack. I would prefer an official solution, or that we rethink our implementation. But I will consider it.

fabian-hiller commented 3 months ago

This will probably be fixed in the next version.

fabian-hiller commented 3 months ago

v0.38.0 is available 🚀