Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Satisfies is very useful as a library author for testing your types. You can write `typetest` files that don't become part of the bundle but are compiled.

One example is that we have a TS library for a JSON-RPC server that is horribly complex on the server side, returning different shapes of output based on input params. I don't think it can be typed with OpenAPI etc, but it can be with Typescript method overloads.

But then instead of writing unit tests that either coerce or mock the server into returning each shape, we can write typetests like this to make sure our type juggling provides the exact right types to developers:

    const rpc = null as unknown as Rpc<SomeApi>
    const result = await rpc.someApi(input).send()
    result satisfies {
        value: {
            nestedFieldForThisInput: number
        }
    }
```

The tradeoff is slightly longer compile times for the library since all this is checked at build time, but it provides a lot of flexibility for testing overloads and utility types in isolation.



You can use multiple tsconfig files to remove the compile time issue; this is quite common for test files anyway. Its not unusual to see one for CI & IDE and one for compilation, the compilation one having a much more limited scope of files.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: