Issue with v7 & TypeScript 3.2 for array or object validation
cyrilletuzi opened this issue · 4 comments
In v7 of the lib, due to this regression in TypeScript 3.2, there is a problem with JSON schemas for objects or array validation if you use the generic JSONSchema
interface:
const schema: JSONSchema = {
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
age: { type: 'number' }
},
required: ['firstName', 'lastName']
};
or if you put a schema for an object or array directly in the method options:
this.localStorage.getItem<User>('user', { schema: {
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
age: { type: 'number' }
},
required: ['firstName', 'lastName']
} });
Other schemas (string, numeric, boolean) are not affected.
Despite it's a regression, TypeScript team visibly doesn't plan to fix this until TypeScript 3.4 (yes, you've heard right...). So it has been decided to not support TypeScript 3.2.
If you're still on Angular 7.0 or 7.1 (so with TypeScript 3.1), there will be no problem with compilation. But as VS Code already uses TypeScript 3.2, errors can appear inside the editor.
If you upgraded to Angular 7.2, stick to TypeScript 3.1. Angular 7.2 still supports TS 3.1, and TS 3.2 adds nothing very interesting (except issues), so there is no need to rush the update. If you upgrade your Angular app via ng update
, the CLI may update to TS 3.2, so revert this with this command:
npm install typescript@3.1 -D
TS 3.2 issue can be bypassed by casting each JSON schema and subschemas, but we do not recommend this option as it requires a lot of work, that may need to be reverted in v8 of this lib.
Version 8 of the lib will fix these TS problems once and for all (already ready in #74). But as it will imply some breaking changes, I have to wait for v8.
Another solution is to go back to v6 of the lib, which supports Angular 7 with TypeScript 3.1 and TypeScript 3.2:
npm install @ngx-pwa/local-storage@6
Sorry for this issue, but it's beyond my control and TS team doesn't care.
Waiting for microsoft/TypeScript#29168
Fixed in #74, which will be released in v8.
Keeping the issue open for v7 documentation.
v8 beta is available for testing, see #76
This is fixed in v8.