ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
6.99k stars 224 forks source link

Type-tests do not check for strict types #1174

Open thoughtspile opened 1 year ago

thoughtspile commented 1 year ago

Hi! I borrowed your typing test suite for my library, banditypes — thanks for the excellent job!

I quickly discovered that test<Expected>() util checks that struct returns type assignable to Expected, not strictly equal to it. This might be dangerous, as a struct might check for a narrower type than expected:

test<string | number>((x) => {
  assert(x, string())
  return x
})

I suggest migrating the suite to expect-type or otherwise checking for strict equality. For reference, here's my MR with the change — it's not too hard.

yeoffrey commented 3 months ago

Might be a way to do this with Vitest, will return to this.

thoughtspile commented 3 months ago

Afaik, vitest uses expect-type under the hood, so this should work as well