From 3a43cc024409b86e6895037d7ef8175a19cc0812 Mon Sep 17 00:00:00 2001 From: pubkey Date: Wed, 19 Sep 2018 19:22:26 +0200 Subject: [PATCH] FIX #448 Does not compile in TypeScript with strict flag enabled --- CHANGELOG.md | 5 ++++ docs-src/tutorials/typescript.md | 2 +- examples/angular2/app/src/RxDB.d.ts | 14 +++++------ .../hero-edit/hero-edit.component.ts | 2 +- .../hero-insert/hero-insert.component.ts | 13 +++++++---- .../heroes-list/heroes-list.component.ts | 7 +++--- .../app/src/pages/home/home.component.ts | 8 ++++--- .../angular2/app/src/schemas/hero.schema.json | 5 ++-- .../app/src/services/database.service.ts | 23 ++++++++++--------- examples/angular2/package.json | 13 ++++++----- examples/angular2/tsconfig.json | 5 ++-- examples/angular2/webpack.config.common.js | 3 +-- test/tutorials/package.json | 8 ++++--- test/tutorials/src/typescript.ts | 2 +- test/tutorials/tsconfig.json | 2 +- test/unit/typings.test.js | 2 +- typings/rx-database.d.ts | 2 +- typings/rx-document.d.ts | 2 +- 18 files changed, 68 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49e03c8ba71..ea7444fb799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +### 8.X.X (comming soon) + +Bugfixes: + - Does not compile in TypeScript with strict flag enabled [#448](https://github.com/pubkey/rxdb/issues/448) + ### 8.0.0 (18. September 2018) BREAKING [read the announcement](./orga/releases/8.0.0.md) Breaking: diff --git a/docs-src/tutorials/typescript.md b/docs-src/tutorials/typescript.md index f74384564b9..21094f17fa5 100644 --- a/docs-src/tutorials/typescript.md +++ b/docs-src/tutorials/typescript.md @@ -39,7 +39,7 @@ We also add some ORM-methods for the document. ```typescript type HeroDocMethods = { - scream: (string) => string; + scream: (v: string) => string; }; ``` diff --git a/examples/angular2/app/src/RxDB.d.ts b/examples/angular2/app/src/RxDB.d.ts index 154852ca8ae..3ed04718a72 100755 --- a/examples/angular2/app/src/RxDB.d.ts +++ b/examples/angular2/app/src/RxDB.d.ts @@ -6,17 +6,17 @@ import { RxDocument, RxCollection, RxDatabase } from 'rxdb'; import { Observable } from 'rxjs'; -declare interface RxHeroDocumentType { - name?: string; - color?: string; - maxHP?: number; - hp?: number; +export type RxHeroDocumentType = { + name: string; + color: string; + maxHP: number; + hp: number; team?: string; - skills?: Array<{ + skills: Array<{ name?: string, damage?: string }>; -} +}; // ORM methods type RxHeroDocMethods = { diff --git a/examples/angular2/app/src/components/hero-edit/hero-edit.component.ts b/examples/angular2/app/src/components/hero-edit/hero-edit.component.ts index 54c531e8764..ef5e5d280a0 100644 --- a/examples/angular2/app/src/components/hero-edit/hero-edit.component.ts +++ b/examples/angular2/app/src/components/hero-edit/hero-edit.component.ts @@ -27,7 +27,7 @@ export class HeroEditComponent implements OnInit { @Output('done') done = new EventEmitter(); public synced: Boolean = true; - public formValue: Number; + public formValue: number; private subs: Subscription[] = []; constructor( diff --git a/examples/angular2/app/src/components/hero-insert/hero-insert.component.ts b/examples/angular2/app/src/components/hero-insert/hero-insert.component.ts index d68162875e2..1eee3ad9f69 100755 --- a/examples/angular2/app/src/components/hero-insert/hero-insert.component.ts +++ b/examples/angular2/app/src/components/hero-insert/hero-insert.component.ts @@ -1,6 +1,5 @@ import { Component, ViewChild, OnInit, ChangeDetectionStrategy } from '@angular/core'; import { DatabaseService } from '../../services/database.service'; -import * as randomInt from 'random-int'; @Component({ selector: 'hero-insert', @@ -11,7 +10,7 @@ import * as randomInt from 'random-int'; }) export class HeroInsertComponent { - @ViewChild('input') inputfield; + @ViewChild('input') inputfield: any; tempDoc: any; @@ -23,7 +22,7 @@ export class HeroInsertComponent { reset() { this.tempDoc = this.dbService.db.hero.newDocument({ - maxHP: randomInt(100, 1000) + maxHP: getRandomArbitrary(100, 1000) }); } @@ -43,6 +42,12 @@ export class HeroInsertComponent { this.inputfield.nativeElement.focus(); } +} - +/** + * Returns a random number between min (inclusive) and max (exclusive) + * @link https://stackoverflow.com/a/1527820/3443137 + */ +function getRandomArbitrary(min: number, max: number): number { + return Math.random() * (max - min) + min; } diff --git a/examples/angular2/app/src/components/heroes-list/heroes-list.component.ts b/examples/angular2/app/src/components/heroes-list/heroes-list.component.ts index f2de4bb0cb3..a7e4f9ab9d8 100755 --- a/examples/angular2/app/src/components/heroes-list/heroes-list.component.ts +++ b/examples/angular2/app/src/components/heroes-list/heroes-list.component.ts @@ -43,14 +43,14 @@ export class HeroesListComponent { ); } - set edit(hero) { + set edit(hero: RxHeroDocument) { console.log('editHero: ' + hero.name); this.editChange.emit(hero); } - editHero(hero) { + editHero(hero: RxHeroDocument) { this.edit = hero; } - deleteHero(hero) { + deleteHero(hero: RxHeroDocument) { hero.remove(); } @@ -60,6 +60,7 @@ export class HeroesListComponent { async foo(): Promise { const db = this.dbService.db; const firstDoc = await db.hero.findOne().exec(); + if (!firstDoc) return 'not found'; const f: string = firstDoc.color; return f; } diff --git a/examples/angular2/app/src/pages/home/home.component.ts b/examples/angular2/app/src/pages/home/home.component.ts index 661feeee8e4..ba22b8d7fec 100755 --- a/examples/angular2/app/src/pages/home/home.component.ts +++ b/examples/angular2/app/src/pages/home/home.component.ts @@ -1,6 +1,8 @@ import { Component } from '@angular/core'; import { RxDocument } from '../../../../../../'; -import * as RxDBTypes from '../../RxDB.d'; +import { + RxHeroDocument +} from '../../RxDB.d'; @Component({ @@ -9,7 +11,7 @@ import * as RxDBTypes from '../../RxDB.d'; }) export class HomeComponent { - editedHero: RxDBTypes.RxHeroDocument; + editedHero: RxHeroDocument; constructor() { } ngOnInit() { } @@ -21,7 +23,7 @@ export class HomeComponent { const x: number = this.editedHero.hpPercent(); } - editHero(hero) { + editHero(hero: RxHeroDocument) { this.editedHero = hero; } } diff --git a/examples/angular2/app/src/schemas/hero.schema.json b/examples/angular2/app/src/schemas/hero.schema.json index 32174d9cb68..5af5eccd8f9 100644 --- a/examples/angular2/app/src/schemas/hero.schema.json +++ b/examples/angular2/app/src/schemas/hero.schema.json @@ -43,8 +43,9 @@ "type": "number" } } - } + }, + "default": [] } }, - "required": ["color", "maxHP"] + "required": ["color", "hp", "maxHP", "skills"] } diff --git a/examples/angular2/app/src/services/database.service.ts b/examples/angular2/app/src/services/database.service.ts index d5b3cee331c..44de601a20b 100755 --- a/examples/angular2/app/src/services/database.service.ts +++ b/examples/angular2/app/src/services/database.service.ts @@ -8,8 +8,10 @@ import { // import typings import { + RxHeroDocument, RxHeroesDatabase, - RxHeroesCollections + RxHeroesCollections, + RxHeroDocumentType } from './../RxDB.d'; @@ -46,7 +48,7 @@ import * as PouchdbAdapterHttp from 'pouchdb-adapter-http'; RxDB.plugin(PouchdbAdapterHttp); -import PouchdbAdapterIdb from 'pouchdb-adapter-idb'; +import * as PouchdbAdapterIdb from 'pouchdb-adapter-idb'; RxDB.plugin(PouchdbAdapterIdb); const useAdapter = 'idb'; @@ -56,7 +58,7 @@ let collections = [ name: 'hero', schema: require('../schemas/hero.schema.json'), methods: { - hpPercent() { + hpPercent(this: RxHeroDocument): number { return this.hp / this.maxHP * 100; } }, @@ -83,7 +85,7 @@ async function _create(): Promise { // password: 'myLongAndStupidPassword' // no password needed }); console.log('DatabaseService: created database'); - window['db'] = db; // write to window for debugging + (window as any)['db'] = db; // write to window for debugging // show leadership in title db.waitForLeadership() @@ -98,10 +100,10 @@ async function _create(): Promise { // hooks console.log('DatabaseService: add hooks'); - db.collections.hero.preInsert(function(docObj) { + db.collections.hero.preInsert(function(docObj: RxHeroDocumentType) { const color = docObj.color; return db.collections.hero.findOne({ color }).exec() - .then(has => { + .then((has: RxHeroDocument | null) => { if (has != null) { alert('another hero already has the color ' + color); throw new Error('color already there'); @@ -110,12 +112,11 @@ async function _create(): Promise { }); }); - // sync + // sync with server console.log('DatabaseService: sync'); - collections - .filter(col => col.sync) - .map(col => col.name) - .forEach(colName => db[colName].sync({ remote: syncURL + colName + '/' })); + await db.hero.sync({ + remote: syncURL + '/hero' + }); return db; } diff --git a/examples/angular2/package.json b/examples/angular2/package.json index 4610dba9d84..8da3ac0940c 100644 --- a/examples/angular2/package.json +++ b/examples/angular2/package.json @@ -36,7 +36,7 @@ "@angular/platform-server": "6.1.7", "@angular/router": "6.1.7", "babel-polyfill": "6.26.0", - "concurrently": "3.5.0", + "concurrently": "4.0.1", "core-js": "2.5.7", "font-awesome": "4.7.0", "hammerjs": "2.0.8", @@ -45,8 +45,7 @@ "normalize.css": "7.0.0", "pouchdb-adapter-http": "7.0.0", "pouchdb-adapter-idb": "7.0.0", - "pouchdb-server": "2.3.7", - "random-int": "1.0.0", + "pouchdb-server": "4.1.0", "regenerator": "0.11.0", "roboto-npm-webfont": "1.0.1", "rxdb": "../../", @@ -54,9 +53,11 @@ "zone.js": "0.8.26" }, "devDependencies": { - "@types/chai": "4.1.4", + "@types/chai": "4.1.5", "@types/core-js": "0.9.43", "@types/hammerjs": "2.0.36", + "@types/pouchdb-adapter-http": "6.1.2", + "@types/pouchdb-adapter-idb": "6.1.2", "@types/requirejs": "2.1.31", "angular2-template-loader": "0.6.2", "appcache-webpack-plugin": "1.4.0", @@ -80,10 +81,10 @@ "raw-loader": "0.5.1", "rimraf": "2.6.2", "style-loader": "0.19.0", - "testcafe": "0.17.1", + "testcafe": "0.22.0", "testcafe-hammerhead": "11.2.1", "tslint": "5.11.0", - "typescript": "2.7.2", + "typescript": "3.0.3", "url-loader": "0.6.2", "webpack": "3.8.1", "webpack-dev-middleware": "1.12.0", diff --git a/examples/angular2/tsconfig.json b/examples/angular2/tsconfig.json index 4e805e3c46c..9fbe5c7c5d4 100755 --- a/examples/angular2/tsconfig.json +++ b/examples/angular2/tsconfig.json @@ -2,7 +2,6 @@ "compileOnSave": false, "buildOnSave": false, "compilerOptions": { - "noStrictGenericChecks": true, "target": "es5", "module": "commonjs", "moduleResolution": "node", @@ -10,7 +9,9 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, - "noImplicitAny": false, + "strict": true, + "strictPropertyInitialization": false, + "strictNullChecks": true, "types": [ "core-js", "requirejs" diff --git a/examples/angular2/webpack.config.common.js b/examples/angular2/webpack.config.common.js index 36401b9cb4b..ac59855b2f0 100755 --- a/examples/angular2/webpack.config.common.js +++ b/examples/angular2/webpack.config.common.js @@ -57,6 +57,5 @@ module.exports = { }, node: { fs: 'empty' - }, - stats: 'none' // disabled to not polute travis-output + } }; diff --git a/test/tutorials/package.json b/test/tutorials/package.json index 60d7e3074b5..b24ab9390a7 100644 --- a/test/tutorials/package.json +++ b/test/tutorials/package.json @@ -10,11 +10,13 @@ "rxdb": "../../" }, "devDependencies": { - "@types/node": "^10.9.4", + "@types/node": "10.9.4", + "@types/pouchdb-adapter-http": "6.1.2", + "@types/request-promise-native": "1.0.15", "async-test-util": "1.6.1", "node": "10.10.0", - "request": "^2.88.0", - "request-promise-native": "^1.0.5", + "request": "2.88.0", + "request-promise-native": "1.0.5", "ts-node": "7.0.1", "typescript": "3.0.3" } diff --git a/test/tutorials/src/typescript.ts b/test/tutorials/src/typescript.ts index 8d65a201a2e..2bf2cb0eb6e 100644 --- a/test/tutorials/src/typescript.ts +++ b/test/tutorials/src/typescript.ts @@ -29,7 +29,7 @@ type HeroDocType = { }; type HeroDocMethods = { - scream: (string) => string; + scream: (v: string) => string; }; type HeroDocument = RxDocument; diff --git a/test/tutorials/tsconfig.json b/test/tutorials/tsconfig.json index 52e1dfa983f..513347cc18d 100644 --- a/test/tutorials/tsconfig.json +++ b/test/tutorials/tsconfig.json @@ -4,7 +4,7 @@ "preserveConstEnums": true, "sourceMap": true, "strictNullChecks": true, - "noImplicitAny": false, + "noImplicitAny": true, "noImplicitReturns": true, "noImplicitThis": true, "strictPropertyInitialization": true, diff --git a/test/unit/typings.test.js b/test/unit/typings.test.js index ab7456ecaa7..2410ca1b4da 100644 --- a/test/unit/typings.test.js +++ b/test/unit/typings.test.js @@ -39,7 +39,7 @@ describe('typings.test.js', () => { const stderr = []; const promise = spawn('ts-node', [ '--no-cache', - '--compilerOptions', '{"target":"es6", "strict": true, "strictNullChecks": true}', + '--compilerOptions', '{"target":"es6", "strict": true, "strictNullChecks": true, "noImplicitAny": true}', '--type-check', '-p', code ]); diff --git a/typings/rx-database.d.ts b/typings/rx-database.d.ts index 92feee95293..209037def27 100644 --- a/typings/rx-database.d.ts +++ b/typings/rx-database.d.ts @@ -71,7 +71,7 @@ export declare class RxDatabaseBase>>; // from rxdb/plugins/server - server(ServerOptions?): { + server(options?: ServerOptions): { app: any; server: any; }; diff --git a/typings/rx-document.d.ts b/typings/rx-document.d.ts index 28e7196a554..4768b30eec9 100644 --- a/typings/rx-document.d.ts +++ b/typings/rx-document.d.ts @@ -16,7 +16,7 @@ export type RxDocument = RxDocumentBase = RxDocumentType & { _rev: string }; -declare type AtomicUpdateFunction = (RxDocumentType) => RxDocumentType | Promise; +declare type AtomicUpdateFunction = (doc: RxDocumentType) => RxDocumentType | Promise; export declare class RxDocumentBase { readonly collection: RxCollection;