hoeck / simple-runtypes

Small, efficient and extendable runtype library for Typescript
MIT License
114 stars 5 forks source link

Fix typing of `json` runtype output #73

Closed remnantkevin closed 2 years ago

remnantkevin commented 2 years ago

Reproduction of issue: codesandbox.io

While using the json runtype for the first time lately, I noticed that, while the runtype was working in terms of producing the correct output, it was losing type information. For example, after adding the tests from this PR, but before changing anything in src/json.ts, if I ran yarn test:types I got the following output:

output ``` ❯ yarn run test:types yarn run v1.22.19 $ tsd test-d/json.test-d.ts:11:21 ✖ 11:21 Argument of type unknown is not assignable to parameter of type number. ✖ 16:21 Argument of type unknown is not assignable to parameter of type string. ✖ 21:23 Argument of type unknown is not assignable to parameter of type number[]. ✖ 26:28 Argument of type unknown is not assignable to parameter of type { a: string; }. test-d/record.test-d.ts:51:57 ✖ 51:57 Argument of type { a: string; b: unknown; d: string; } is not assignable to parameter of type { a: string; b: { c: string; }; d: string; }. Types of property b are incompatible. Type unknown is not assignable to type { c: string; }. 5 errors error Command failed with exit code 1. ```

And in VS Code I can see the following in the json tests that already exist in test/json.test.ts:

screenshot ![2022-08-07-11 02](https://user-images.githubusercontent.com/8270635/183273122-c57815e1-4ef1-4f98-b775-c23c48bbd8ee.png)

After making this PR's changes in src/json.ts, the tests pass and I see the following in VS Code:

screenshot ![2022-08-07-11 16](https://user-images.githubusercontent.com/8270635/183273470-f03262af-9ac6-4e96-afe9-8c856180b72f.png)