ostrowr / ts-json-validator

Let JSON play nicely with Typescript
MIT License
342 stars 7 forks source link

Bug: type checking via `Validated` is not working as expected #19

Closed cogell closed 4 years ago

cogell commented 4 years ago

See the following small example of the error:

import { createSchema as S, Validated } from 'ts-json-validator';

const MySchema = S({
  type: 'object',
  properties: { field1: S({ type: 'string' }) },
  required: ['field1'],
});

// typescript thinks MySchemaType is `never` type
type MySchemaType = Validated<typeof MySchema>;

const myObj: MySchemaType = {
  // expect typescript error here
  field1: 1, 
};

https://codesandbox.io/s/modest-mccarthy-zyjdq

ostrowr commented 4 years ago

Thanks! I can't replicate this on 3.7.x – unfortunately, this library depends on some recursive type shenanigans that don't work until 3.7. Closing for now but feel free to reopen if (a) you have any ideas about how to get this working on < 3.7 or (b) you see the same issue while using Typescript 3.7+.

cogell commented 4 years ago

Oh shoot, you're right! Thanks for the help. @ostrowr 🙇‍♂️

For anyone else running into this, VSCode does NOT automatically use your locally installed typescript version. To set your VSCode workspace to use local typescript see this stackoverflow answer: https://stackoverflow.com/a/39676463/1873778