Skip to content

Commit

Permalink
FIX #448 Does not compile in TypeScript with strict flag enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Sep 19, 2018
1 parent afb292b commit 3a43cc0
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 50 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs-src/tutorials/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ We also add some ORM-methods for the document.

```typescript
type HeroDocMethods = {
scream: (string) => string;
scream: (v: string) => string;
};
```

Expand Down
14 changes: 7 additions & 7 deletions examples/angular2/app/src/RxDB.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -11,7 +10,7 @@ import * as randomInt from 'random-int';
})
export class HeroInsertComponent {

@ViewChild('input') inputfield;
@ViewChild('input') inputfield: any;

tempDoc: any;

Expand All @@ -23,7 +22,7 @@ export class HeroInsertComponent {

reset() {
this.tempDoc = this.dbService.db.hero.newDocument({
maxHP: randomInt(100, 1000)
maxHP: getRandomArbitrary(100, 1000)
});
}

Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -60,6 +60,7 @@ export class HeroesListComponent {
async foo(): Promise<string> {
const db = this.dbService.db;
const firstDoc = await db.hero.findOne().exec();
if (!firstDoc) return 'not found';
const f: string = firstDoc.color;
return f;
}
Expand Down
8 changes: 5 additions & 3 deletions examples/angular2/app/src/pages/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component } from '@angular/core';
import { RxDocument } from '../../../../../../';
import * as RxDBTypes from '../../RxDB.d';
import {
RxHeroDocument
} from '../../RxDB.d';


@Component({
Expand All @@ -9,7 +11,7 @@ import * as RxDBTypes from '../../RxDB.d';
})
export class HomeComponent {

editedHero: RxDBTypes.RxHeroDocument;
editedHero: RxHeroDocument;

constructor() { }
ngOnInit() { }
Expand All @@ -21,7 +23,7 @@ export class HomeComponent {
const x: number = this.editedHero.hpPercent();
}

editHero(hero) {
editHero(hero: RxHeroDocument) {
this.editedHero = hero;
}
}
5 changes: 3 additions & 2 deletions examples/angular2/app/src/schemas/hero.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
"type": "number"
}
}
}
},
"default": []
}
},
"required": ["color", "maxHP"]
"required": ["color", "hp", "maxHP", "skills"]
}
23 changes: 12 additions & 11 deletions examples/angular2/app/src/services/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {

// import typings
import {
RxHeroDocument,
RxHeroesDatabase,
RxHeroesCollections
RxHeroesCollections,
RxHeroDocumentType
} from './../RxDB.d';


Expand Down Expand Up @@ -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';

Expand All @@ -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;
}
},
Expand All @@ -83,7 +85,7 @@ async function _create(): Promise<RxHeroesDatabase> {
// 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()
Expand All @@ -98,10 +100,10 @@ async function _create(): Promise<RxHeroesDatabase> {

// 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');
Expand All @@ -110,12 +112,11 @@ async function _create(): Promise<RxHeroesDatabase> {
});
});

// 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;
}
Expand Down
13 changes: 7 additions & 6 deletions examples/angular2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -45,18 +45,19 @@
"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": "../../",
"rxjs": "6.3.2",
"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",
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions examples/angular2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
"compileOnSave": false,
"buildOnSave": false,
"compilerOptions": {
"noStrictGenericChecks": true,
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"strict": true,
"strictPropertyInitialization": false,
"strictNullChecks": true,
"types": [
"core-js",
"requirejs"
Expand Down
3 changes: 1 addition & 2 deletions examples/angular2/webpack.config.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ module.exports = {
},
node: {
fs: 'empty'
},
stats: 'none' // disabled to not polute travis-output
}
};
8 changes: 5 additions & 3 deletions test/tutorials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion test/tutorials/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type HeroDocType = {
};

type HeroDocMethods = {
scream: (string) => string;
scream: (v: string) => string;
};

type HeroDocument = RxDocument<HeroDocType, HeroDocMethods>;
Expand Down
2 changes: 1 addition & 1 deletion test/tutorials/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"preserveConstEnums": true,
"sourceMap": true,
"strictNullChecks": true,
"noImplicitAny": false,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"strictPropertyInitialization": true,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/typings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
Expand Down
2 changes: 1 addition & 1 deletion typings/rx-database.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export declare class RxDatabaseBase<Collections= { [key: string]: RxCollection }
getLocal(id: string): Promise<RxLocalDocument<RxDatabase<Collections>>>;

// from rxdb/plugins/server
server(ServerOptions?): {
server(options?: ServerOptions): {
app: any;
server: any;
};
Expand Down
2 changes: 1 addition & 1 deletion typings/rx-document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type RxDocument<RxDocumentType, OrmMethods = {}> = RxDocumentBase<RxDocum

export type RxDocumentTypeWithRev<RxDocumentType> = RxDocumentType & { _rev: string };

declare type AtomicUpdateFunction<RxDocumentType> = (RxDocumentType) => RxDocumentType | Promise<RxDocumentType>;
declare type AtomicUpdateFunction<RxDocumentType> = (doc: RxDocumentType) => RxDocumentType | Promise<RxDocumentType>;

export declare class RxDocumentBase<RxDocumentType, OrmMethods = {}> {
readonly collection: RxCollection<RxDocumentType, OrmMethods>;
Expand Down

0 comments on commit 3a43cc0

Please sign in to comment.