Closed moltar closed 5 years ago
That's odd. Which version of TypeScript are you using in your IDE? Intellisense should infer
schema
validate
parse
There's no data
anywhere.
I've made a bunch of changes in the last 2 days, can you try latest master and see if you're still running into this issue?
The generated code looks right, but can you check if the generated types match as well? tsjson-parser.d.ts should have something like
export declare class TsjsonParser<T extends SchemaLike> {
readonly schema: T;
private readonly validator;
constructor(schema: T);
validate(data: unknown): asserts data is Validated<T>;
parse: (text: string, skipValidation?: boolean) => T[typeof InternalTypeSymbol];
}
You can see this use here, with all of the deps defined in package.json:
The IDE I'm using is VS Code (latest)
Just cloned your repo and was able to replicate.
Looks like you're using Typescript 3.6.4 (which is a totally reasonable thing to be doing.)
However, ts-json-validator
currently requires 3.7+ in order to work properly. (As I warn in the README)
When npm-installing in your repo, I get
npm WARN ts-json-validator@0.1.1 requires a peer of typescript@>= 3.7.0 but none is installed. You must install peer dependencies yourself.
Which I think is enough warning – but if you have an idea to make this more obvious, I'd welcome a suggestion/PR!
I've confirmed that this works in the IDE using 3.7 in your repo.
Closing this but feel free to reopen if you think I've missed something.
Ok, thanks for looking into this. What features of TS 3.7 is it relying on that are not available in 3.6.4? Thanks.
Interestingly, after the update, the tests are failing.
export function caseTsJsonValidator(data: Data) {
return parser.validate(data)
}
It seems return type of the function cannot be inferred and is set to void
for some odd reason.
Even though when hovering the return type of validate
in IDE, it shows it as a correct return type.
Any ideas?
That's due to https://github.com/microsoft/TypeScript/issues/34596
I'm going to change this to a normal type guard for now (so .validate will return true
if it validates, and will narrow the type, and false
if not.)
This:
Gets compiled to:
And IDE cannot infer this as an available class method.